int * matrixsum(int *a,int *b,int n,int m)
{
int *p=NULL,i,j;
p=malloc(sizeof(int)*n*m);
if(p==NULL)
{
printf("Error!\n");
exit(1);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
*(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);
}
}
return p;
}
My question is about the line *(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);: if I replace it with p[i][j]=a[i][j]+b[i][j]; I get the following error 3 times:
error: subscripted value is neither array nor pointer nor vector
Why? From my knowledge they are the same thing.
My compiler is gcc version 4.6.3.
p[i * n +j]etc. You only have a one-dimensional array.*(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);should useminstead ofn.