J
Joonshik Kim
I was trying to define 3d array with pointer to pointer. I wrote like
following.
int ***d;
nx = 3;
ny = 5;
nz = 4;
d = (int ***)malloc((int) nx*sizeof(int **));
*d = (int **)malloc((int) nx*ny*sizeof(int*));
**d = (int *)malloc((int) nx*ny*nz*sizeof(int));
for(i = 1; i < nx ; i++) d = d[i-1] + ny;
for(i = 0; i < nx ; i++)
for(j = 1; j < ny ; j++) d[j] = d[j-1] + nz;
for(i=0 ; i < nx; i ++)
for(j = 0; j < ny; j++)
for(k = 0 ; k < nz; k++)
d[j][k] = 100*(i+1) + 10*(j+1) + k;
it compiled ok. but when I ran it, it shows error message like
"segementation fault.."
What did I do wrong ? can someone help me ?
thank you.
following.
int ***d;
nx = 3;
ny = 5;
nz = 4;
d = (int ***)malloc((int) nx*sizeof(int **));
*d = (int **)malloc((int) nx*ny*sizeof(int*));
**d = (int *)malloc((int) nx*ny*nz*sizeof(int));
for(i = 1; i < nx ; i++) d = d[i-1] + ny;
for(i = 0; i < nx ; i++)
for(j = 1; j < ny ; j++) d[j] = d[j-1] + nz;
for(i=0 ; i < nx; i ++)
for(j = 0; j < ny; j++)
for(k = 0 ; k < nz; k++)
d[j][k] = 100*(i+1) + 10*(j+1) + k;
it compiled ok. but when I ran it, it shows error message like
"segementation fault.."
What did I do wrong ? can someone help me ?
thank you.