Can anyone explain to me why I get a segmentation fault here?
(I'm writing a longer program that involves adding and multiplying matrices saved as dynamic arrays, but I tried to narrow down the scope of the program looking for the error - so don't worry that the excerpt below doesn't make too much sense, I just want to know what is wrong syntactically with it.)
int
main (void)
{
int* a;
int* c;
int i,j,d;
int n = 3;
int m = n*n;
a = (int*)malloc(m*sizeof(int));
c = (int*)malloc(m*sizeof(int));
a[0] = 1; a[1] = 4; a[2] = 3; a[3] = 2; a[4] = 2; a[5] = 2; a[6] = 0; a[7] = 1;
a[8] = 0;
for (i = 0; i<n; ++i)
{
for (j = 0; i<n; ++j)
{
d = i*n + j;
c[d] = a[d] + a[d];
}
}
return 0;
}