I realized the following problem:
struct S{
double **A;
}
struct S s;
s.A = (double **) malloc(sizeof(double*)*2);
s.A[0] = (double *) malloc(sizeof(double)*3);
s.A[1] = (double *) malloc(sizeof(double)*3);
Now let us say I want to do pointer arithmetic of s.A
.
If I start by defining a pointer to a double like:
double ptr
ptr = &s.A[0][0];
I will expect that
*(ptr+1)
is equivalent to s.A[0][1]
*(ptr+2)
is equivalent to s.A[0][2]
*(ptr+3)
is equivalent to s.A[1][0]
I realize that *(ptr+3)
is NOT equivalent to s.A[1][0]
and points to a wrong address. To point to s.A[1][0]
I need to do *(ptr+4)
, so jumping with 1. Can anyone explain why I need to jump an extra one when I want to begin at the next row, and how will I do it if I had to start at the third, fourth etc rows?