help in uderstaning 2D array

This is a discussion on help in uderstaning 2D array within the C Programming forums, part of the General Programming Boards category; please help me in understanding these code snippets- 1) Code: int main () { int a[2][2]={1,2,3,4}; a[1][0]=*(*(a+1)+1); // how this ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    help in uderstaning 2D array

    please help me in understanding these code snippets-

    1)
    Code:
    int main ()
    {
    int a[2][2]={1,2,3,4};
    a[1][0]=*(*(a+1)+1); // how this works??
    a[1][1]=*(*(a+1)+0);
    
    printf("%d %d %d %d\n",a[0][0],a[0][1],a[1][0],a[1][1]);
    return 0;
    }
    and the second one is

    Code:
    int main ()
    {
    int p[10];
    printf("%d",&p+1);
    }
    why it prints -10

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The second one is quite simple. Open a phone book. Close your eyes and point to a phone number, on some page.

    Why does it show that number? That's why it prints -10 (or whatever).

    The first snippet, has to do with a. What is a? An address of the first element in the a array.

    Now ask yourself, what is the value of a? Have your program print it out for you. Now have it show you what the value of a+1 is. Now have it show you the value of *(a+1), and *a +1, and all the rest.

    Work with it until you're satisfied that you KNOW what these expressions mean (and don't mean).

    When it's feasible, don't hesitate to enhance your programming skills by experimenting. Learning by doing is a great way to let it really sink in.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    4

    Pointer Arithmetic

    In case of first snippet You just accessing array element by specifying its row and column value.

    Code:
    int a[2][2]={1,2,3,4};
    
    //  defined array structure 
    // a  0 1
    // 0  1 2  
    // 1  3 4 
    
    a[1][0]=*(*(a+1)+1); // how this works?? 
    //  In above code, first *(a+1) is accessing the second rows of the array
    //  Once you reach to second rows , you are accessing the second element of the array 
    //  So , it is equal to a[1][1] 
    a[1][1]=*(*(a+1)+0);
    //   If you understand first one, So above statement is equal to a[1][0]

    In case of second snippet :

    Actually you have not given specific values for that array.
    So ,it is accessing some garbage value.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    The second one giving a negative number shouldn't even be possible. Perhaps a signed/unsigned conversion error?
    Unless the & is just a typo for *, then it would just be uninitialized data as Adak and pavun_cool pointed out.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int main ()
    {
    int p[10];
    printf("%d",&p+1);
    }
    There is nothing undefined with that statement.
    its printing the address of p[10] ( address of p + size of the array ) interpreted as int.
    Kurt

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    Quote Originally Posted by ZuK
    There is nothing undefined with that statement.
    its printing the address of p[10] ( address of p + size of the array ) interpreted as int.
    That corresponds with _Mike's observation, and I think that is indeed what happened. However, I also think that it does result in undefined behaviour: the argument is supposed to be of the correct type for the corresponding conversion specification, but that is not the case here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    16
    Code:
    int main() {
    
            int a[2][2] = { 1, 2, 3, 4 };
            printf( "%ld %ld\n", a + 1 , *(a + 1));
            return 0;
    }
    It gives the output
    140734762639080 140734762639080

    Is a + 1 and *( a + 1) are same ?? . If not, how ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    Quote Originally Posted by lsme
    Is a + 1 and *( a + 1) are same ?? . If not, how ?
    They have the same value, but they are not the same.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21