无效打印_数组未按exprected格式工作

无效打印_数组未按exprected格式工作,c,arrays,C,Arrays,我用C语言编写了这段代码: #include <stdio.h> #include <stdlib.h> #include <math.h> void print_array(char** array, int height, int width); int main() { int height, width; char **array = 0; printf("Give height board size:"); sc

我用C语言编写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_array(char** array, int height, int width);

int main()
{
    int height, width;
    char **array = 0;

    printf("Give height board size:");
    scanf("%d", &height);
    printf("Give width board size:");
    scanf("%d", &width);

    print_array(array, height, width);


    return 0;
}

void print_array(char** array, int height, int width)
{
    int i, j;

    printf("\n |"); for (j = 0; j < width; j++) printf("-");
    printf("|\n");
    for (i = 0; i < height; i++)
    {
        printf(" |");
        for (j = 0; j < width; j++) printf("%c", array[i][j]);
        printf("|\n");
    }
    printf(" |"); for (j = 0; j < width; j++) printf("-");
    printf("|");
}
但不管我给身高多少,实际结果是

例如高度10和宽度20

|--------------------|
|
如果我使用VisualStudio运行,我实际上会在第32行得到错误代码,如下所示

Exception thrown: read access violation. array was 0x1110112.
第32行

for (j = 0; j < width; j++) printf("%c", array[i][j]);
用于(j=0;j
数组
是指向指针的未初始化指针。试图通过调用的
[]
运算符取消对该数组的引用

这里根本不需要数组。只需打印一个空格:

printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|\n");
printf(“|”);
对于(j=0;j
由于您将
数组
声明为指向
字符
指针的指针,但没有在只读内存中定义它作为声明的同一行,因此必须使用函数
malloc
首先分配数组,然后再
释放它:

int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
    array[i] = malloc(width);

// Use array....

for(i = 0; i < height; i++)
    free(array[i]);

free(array);
inti;
数组=malloc(sizeof(char*)*高度);
对于(i=0;i
如果你真的需要一个二维数组。对于打印出任意宽度和高度的正方形的用例,您不需要

还要注意的是,将
高度
分配为第一个维度,然后将
宽度
分配为第二个维度的选择是完全任意的,也可能是相反的选择。

可能对您有所帮助。它给出了一个类似于@Govind Parmar的答案,但使用此解决方案,数组将不会分配连续的内存区域,这可能会给假设此情况的函数带来问题

相反,您可以:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_array(char** array, int height, int width);

int main()
{
    int height, width;
    char** array;
    char* temp;

    printf("Give height board size:");
    scanf_s("%d", &height);
    printf("Give width board size:");
    scanf_s("%d", &width);

    array = malloc(height * sizeof(char*));
    temp = malloc(height * width * sizeof(char*));

    for (int i = 0; i < height; i++) {
        array[i] = temp + (i * width);
    }

    print_array(array, height, width);

    free(temp);
    free(array);

    return 0;
}

void print_array(char** array, int height, int width)
{
    int i, j;

    printf("\n |"); for (j = 0; j < width; j++) printf("-");
    printf("|\n");
    for (i = 0; i < height; i++)
    {
        printf(" |");
        for (j = 0; j < width; j++) printf("%c", array[i][j]);
        printf("|\n");
    }
    printf(" |"); for (j = 0; j < width; j++) printf("-");
    printf("|");
}
//ConsoleApplication1.cpp:定义控制台应用程序的入口点。
//
#包括
#包括
#包括
无效打印数组(字符**数组,整数高度,整数宽度);
int main()
{
int高度、宽度;
字符**数组;
字符*温度;
printf(“给出高度板尺寸:”);
扫描单位(“%d”和高度);
printf(“给出宽度板尺寸:”);
扫描单位(“%d”和宽度);
数组=malloc(高度*sizeof(字符*);
温度=malloc(高度*宽度*大小)(字符*);
对于(int i=0;i
char**array
不是数组,它是指向
char
的指针。为什么还要数组?你不能只使用
高度
宽度
?我怀疑期望是(不存在的)“数组”已经加载了空格,函数的目标是显示错误信息的事实。否则,该函数的名称会更合适,
print\u box
=O
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_array(char** array, int height, int width);

int main()
{
    int height, width;
    char** array;
    char* temp;

    printf("Give height board size:");
    scanf_s("%d", &height);
    printf("Give width board size:");
    scanf_s("%d", &width);

    array = malloc(height * sizeof(char*));
    temp = malloc(height * width * sizeof(char*));

    for (int i = 0; i < height; i++) {
        array[i] = temp + (i * width);
    }

    print_array(array, height, width);

    free(temp);
    free(array);

    return 0;
}

void print_array(char** array, int height, int width)
{
    int i, j;

    printf("\n |"); for (j = 0; j < width; j++) printf("-");
    printf("|\n");
    for (i = 0; i < height; i++)
    {
        printf(" |");
        for (j = 0; j < width; j++) printf("%c", array[i][j]);
        printf("|\n");
    }
    printf(" |"); for (j = 0; j < width; j++) printf("-");
    printf("|");
}