1. ## 2d array question

Hi all,

I thought I understood using pointers in arrays but it seems I don't! - maybe you can explain.

Declaring and initialising a simple 3x3 matrix:

PHP Code:
``` unsigned int myArray[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};  ```
Why do I get the same address when I do the following:

PHP Code:
``` printf("\n%d %d %d", myArray, &myArray, *myArray);  ```
Finally, how can I explicitly declare a pointer-to-a-pointer, e.g. myArrayPtr, to point to the start of myArray and have the ability to access array elements using the following notation *(*(myArrayPtr + 1) + 1) - in this case element at (2,2).

I realise that using *(*(myArray + 1) + 1) works, but I just want to understand how to declare pointer to the memory block occupied by myArray and access using the same notation.

regards

2. myArray:
myArray is the starting address for the first element in the whole multidimensional array.

&myArray:
myArray, is an array (of multiple dimensions) and arrays automatically expose the address to their first element, thus, &myArray is unneccesary, but has the same effect without the ampersand.

*myArray:
dereferencing once with the operator * is equal to doing myArray[0] - it's an access to the contents of the first element in the whole array, which it's contents is an array,.. of elements of type int. Since myArray is a static array of arrays, the contents being yielded by one dereference is still *is* an array, that is why you're see'ing the same address printed. myArray[x][x] accesses a final-value (an int) stored in a element of a particular array in the multidimensional array while myArray[x] accesses a particular array in the whole multidimensional array.

*myArray:
*myArray and myArray[0] are both equal and they are accessing the contents of the first element of the whole multidimensional array. myArray[x] is specifying which array of the 3 arrays to work on. myArray[x][x] specifies the same thing, but the last index specifies which element in the particular array being accessed (by the first index), it is that you want to acccess, or get.

Thus, *myArray and myArray[0] are both specifying a particular array and they therefore yield the address of that particular array. myArray[0] through myArray[2] will yield 3 different addresses and the first element of each (e.g.: myARray[x][0]) of these is also the same address as the array itself when applied the ampersand, but without it, yield the value at the first element in the current array.

To point to this array with a pointer. You have to only specify in your code, how many elements there are in the array, since the array is a fixed constant size at compile time (they're all 3 elements long), you would do:

unsigned int (*p)[3] = myArray;

The reason for this is that, p is going to allow you to use the same notation as with myArray (e.g.: p[x][x] == myArray[x][x]), since p is a pointer, it does not know, or have any information about jumping acrosss arrays from array 1 to array 3 (0 through 2) so it *must* know how many elements long each array is, therefore the requirement of telling it this is in the pointer declaration to myArray (unsigned int (*p)[3] = myArray), the 3 gives the pointer enough information for it to know how many elements to jump in order to get to the next array when you do p[x]. information for the last index is irrelevant, only the first is relevant because since p was declared as an int type, it knows each element in the array is a size of an int.

Think about how all this fits together, think hard, and you'll get it.