小编vai*_*ter的帖子

我可以用这种方式制作动态阵列吗?

所以我的代码是这样的:

int a, b;
printf("Give dimensions\n");
scanf("%d %d", &a, &b);
double array1[a][b];
printf("Give values\n");
int i, j;
for(i=0; i<a; i++)
{
    for(j=0; j<b; j++)
    {
        scanf("%lf", &array1[i][j]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,我被告知这是一种分配内存的错误方法,我应该使用malloc.我应该使用用户的维度创建一个动态数组.

编辑:程序的其余部分:

double sum=0;
for(i=0; i<a; i++)
{
    for(j=0; j<b; j++)
    {
        sum = sum + array1[i][j];
    }
    printf("Line %d: sum = %lf\n", i+1, sum);
    sum=0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays dynamic-memory-allocation variable-length-array

4
推荐指数
3
解决办法
224
查看次数