Thread: 2 - D arrays & pointers

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    43

    2 - D arrays & pointers

    Here is a code snippet :

    Code:
    int a[2][2] = {
                        {7, 12},
                        {2, 7} 
                      }; 
    
    printf("\n%u %u %u", a + 1, *(a + 1), a[1]);
    All 3 would return the same value i.e., address of 1th 1 - D array.

    As far as I know, when the compiler encounters a reference to 2 - D array, in this case, for instance, 'a' it decays to int (*a)[2]. When I say a + 1, it moves to 1th 1 - D array, but when I say *(a + 1), instead of giving the output 2, it again gives me the address & not the value because all that is happening by saying *(a + 1) is, that it is being converted from pointer to array to just a pointer. Is that right ?

    The book I referred, seems to have a vague explanation, so I just need to confirm.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *(a + 1) is equivalent to a[1], so let's consider a[1]. Wait, there's not much to consider: it is just the element at index 1 for a, i.e., an array of 2 ints, which is convertible to a pointer to int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Arrays and pointers
    By jalex39 in forum C Programming
    Replies: 1
    Last Post: 10-01-2008, 01:09 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Arrays of Pointers
    By phatslug in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2002, 12:04 AM
  5. pointers and arrays
    By sscook69 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2002, 05:00 PM