Wednesday, June 4, 2008

Pointers to pointers in C and multidimensional arrays

An int:
int x = 4

A pointer to an int:
int *intptr = &x

A pointer to an int pointer
int **intptrptr = &intptr


Another way of looking at a multidimensional array such as myarray[2][4] is two pointers to an array of 4 elements each. So just as removing the braces from a regular array gives you a pointer to the first element of the array, removing the second set of braces from a multidimensional array (in this case myarray[0] and myarray[1]) gives you a pointer to the first element of each array.

No comments: