Acces违规写入位置c,2D动态数组

时间:2016-01-12 16:37:12

标签: c arrays computer-science

亲爱的用户我试图将数据写入数组但由于某种原因我一直都会收到此错误..请帮帮我

int** input_array_dyn1(int n,int m) 
{
int i,**a; //index and array we want to set up.
a=(int**)calloc(n,sizeof(int*)); //get room for array in size n of ints.
assert(a); //checking that we have space.
for (i=0;i<n;i++)  //loop for getting value in each cell...
{
a[i]=(int*)calloc(m,sizeof(int)); //get room for array in size n of ints.
assert(a[i]); //checking that we have space.
}
return a; //returning the new array we made.
}

void randomMat(int** a,int n,int m)
 {
int i,j;
for(i=1;i<=n;i++)           
    for(j=1;j<=m;j++)
        a[i][j]=rand()%2;

printf ("you'r first matrix is:\n");
//print_mat(a,m,n);
}

我总是从Acces违规写入位置的行a[i][j]=rand()%2;收到错误 请帮帮我们!

1 个答案:

答案 0 :(得分:1)

您可以看到input_array_dyn1randomMat

中的循环存在差异

比较例如

for (i=0;i<n;i++)  //loop for getting value in each cell...
{
a[i]=(int*)calloc(m,sizeof(int)); //get room for array in size n of ints.
assert(a[i]); //checking that we have space.
}

for(i=1;i<=n;i++)           
    for(j=1;j<=m;j++)
        a[i][j]=rand()%2;

如果数组具有n个元素,则索引的有效范围为[0, n-1]