【发布时间】:2012-10-09 00:11:27
【问题描述】:
可能重复:
How do I correctly set up, access, and free a multidimensional array in C?
我正在尝试使用 calloc 为二维数组动态分配内存。列固定为 2,因此只有动态行。
这是我一直在尝试的:
unsigned int **pts, rows;
int main()
{
//some code
pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **));
}
//The code to access the array :
for(k=1;k<=i;k++)
{
printf("\nX%d=",k);
scanf("%d",&pts[k][0]);
printf("\nY%d=",k);
scanf("%d",&pts[k][1]);
}
但问题是,在访问数组时,程序崩溃了。 我正在使用带有 MinGW GCC 的 Eclipse。
如果我需要在这里添加更多数据或告诉我如何处理这个问题,请告诉我,因为这是我的第一篇文章。
【问题讨论】:
标签: c calloc dynamic-allocation