Thread: 2d array question

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    4

    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] = {{123}, {456}, {789}}; 
    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. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    4
    Thanks for your reply - very informative!!

    regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  2. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM