currently i'm learning about pointers.
mat3 = (double***) malloc(m*sizeof(double**));
This statement indicates that mat3 points to the first array of the two arrays that contain double**.
mat3[0] = (double**) malloc(m*n*sizeof(double*));
I made a small figure to show how I see this. I don't understand why the line with the comment is wrong. In my opinion the same happens as the one above.
for (i=0; i<m; i++)
{
mat3[i] = mat3[0] + i*n;
for(j=0; j<n; j++)
{
mat3[i][j] = mat3[i][0] + j*p; // this goes wrong
}
}
In the first loop the adress of the first ** array taken and each iteration 3 is added. The figure also shows that there are 3 * arrays, so that looks OK.
In the second loop the adress of the first * array is taken and each iteration 4 is added. That also looks okay since each * array contains 4 places to store a double. So 4 memory places needs to be skipped.
There is something wrong, but I don't see it. Can I get feedback?
#include <stdio.h>
#include <stdlib.h>
int main()
{
double*** mat3;
int m,n,p;
int i,j,k;
m=2; n=3; p=4;
printf("This program causes a coredump, unless you fix the error in it !\n");
mat3 = (double***) malloc(m*sizeof(double**));
if (mat3 == NULL)
exit(1);
mat3[0] = (double**) malloc(m*n*sizeof(double*));
if (mat3[0] == NULL)
exit(1);
mat3[0][0] = (double*) malloc(m*n*p*sizeof(double));
if (mat3[0][0] == NULL)
exit(1);
for (i=0; i<m; i++)
{
mat3[i] = mat3[0] + i*n;
for(j=0; j<n; j++)
{
mat3[i][j] = mat3[i][0] + j*p; // this goes wrong
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
for(k=0;k<p;k++)
mat3[i][j][k] = i + 10*j + 100*k;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
for(k=0;k<p;k++)
printf("mat3[%d][%d][%d] = %lf\n",i,j,k,mat3[i][j][k]);
exit(0);
}