【发布时间】:2019-03-10 17:35:12
【问题描述】:
我在网上看到的所有解决方案都使用了两次 calloc() 函数,是否可以只使用一次? 下面的代码没有打印正确的数组元素
int **ptr;
//To allocate the memory
ptr=(int **)calloc(n,sizeof(int)*m);
printf("\nEnter the elments: ");
//To access the memory
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",ptr[i][j]);
}
}
【问题讨论】:
-
您的代码的问题是,指针如何知道数组的尺寸?它需要这些维度来计算正确的内存地址。这就是为什么它只能直接使用一维数组。
-
Correctly allocating multi-dimensional arrays@Lundin Hammer time 的可能重复
-
@Stargateur 我尽量避免欺骗自己发布的问题/答案,除非它们是社区 wiki 等。
标签: c multidimensional-array dynamic