这不是整个程序,只是它的片段(据我所见)导致问题,如果需要更多我会更新
我需要得到一个0的矩形数组才能模拟网格
int Length, Height, NumberOfParticles, Direction, validInput, StartXPosition, StartYPosition;
int *LatticeHeight;
int **Lattice;
LatticeHeight = (int*)malloc(Height*sizeof(int));
/*
Check for memory allocation failure
*/
if(LatticeHeight==NULL)
{
printf("\nMemory allocation failure. Exiting ...\n");
return(EXIT_FAILURE);
}
Lattice = (int**)malloc(Length*sizeof(LatticeHeight));
/*
Check for memory allocation failure again
*/
if(Lattice==NULL)
{
printf("\nMemory allocation failure. Exiting ...\n");
return(EXIT_FAILURE);
}
然后有一些我遗漏的输入验证,但最终它获得了高度和长度的值
int i,j;
for(i=1; i<Height; i++)
{
LatticeHeight[i] = 0;
}
for(j=0; j<Length; j++)
{
Lattice[j] = LatticeHeight;
}
for(i=0;i<Height;i++)
{
for(j=0;j<Length;j++)
{
printf("%d\n",Lattice[i][j]);
}
}
此print语句返回
139064
0
0
0
139064
0
0
0
139064
0
0
0
139064
0
0
0
有人可以解释原因吗?
答案 0 :(得分:2)
您已从1
for(i=1; i<Height; i++)
{
LatticeHeight[i] = 0;
}
将其更改为此
for(i=0; i<Height; i++)
{
LatticeHeight[i] = 0;
}