I am not entirely clear on how to do pointers to multidimensional arrays. It seems that if p is a pointer to the first element of the multidimensional array then you cannot properly use the "p[i][j]" notation (my compiler returns an error, something along the lines of "subscripted value is neither an array nor a pointer"). Instead, you have to do a pointer to the first element of the array viewed as a 1-dim array of 1-dim arrays, which makes the typedefs somewhat more complicated. Furthermore, if you have a function which only knows the pointer to the first real element, you cannot use this trick and you have to do some arithmetic to work out the value. I am sure there must be a better way to do this.
This prints the correct answer of 4, but:Code:#include <stdio.h>
typedef int Test[8];
int *TestFn(int *t)
{
t[11] = 4;
return t;
}
int main ()
{
Test *p;
Test k[8];
int *t;
p = &k[0];
t = &k[0][0];
t = TestFn(t);
printf("%d", p[1][3]);
getchar();
return 0;
}
1. The line "t[11] = 4" is rather cryptic. Obviously we know it is right because 1*8 + 3*1 = 11, but someone else might not know. Is there a way to put a double subscript to show the actual array position? As I commented earlier, "t[1][3]" doesn't seem to work.
2. It is not entirely clear that k is a two-dim array. Obviously I would like to have a definition like:
but I then could not see what type to set p as. Is there a better way around this one too?Code:int k[8][8];
Adam.