Thread: Multidimensional arrays

  1. #46
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Niels_M View Post
    If I use

    Code:
    	int asd[2][3]={{1,2,5},{3,4,6}};
    then what is the difference between asd+1 and *(asd+1)? The first is the address of the {3,4,5}-array, and *(asd+1) "accesses" the array or what?
    (asd+1) points to the 2nd element of asd ie asd[1] itself an array
    Code:
    (asd+1) = &asd[1];
    *(asd+1) points to the first element of the array asd[1] ie asd[1][0]
    Code:
    *(asd+1) = &asd[1][0];

  2. #47
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Sebastiani View Post
    Code:
    	printf( "address of data: %x \n", ( size_t* )data );
    	printf( "address of data + 1: %x \n", ( size_t* )( data + 1 ) );
    %p is the correct format for pointers, and it expects a void pointer. Alternatively, cast to unsigned, and continue to use %x.

    Using the wrong format specifiers for printf is undefined behavior.
    Last edited by King Mir; 09-12-2009 at 01:27 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #48
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by itCbitC View Post
    *(asd+1) points to the first element of the array asd[1] ie asd[1][0]
    Code:
    *(asd+1) = &asd[1][0];
    That is what I meant by "accessing" the array. But OK, I guess this is just how it is with 2D-arrays.

    I just thought there was a logical explanation like there is with string literals, i.e. that in
    Code:
    *test[] = {"hi", "mi"}
    , then test[0] points to the first character in the string literal "hi" etc..

  4. #49
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Niels_M View Post
    That is what I meant by "accessing" the array. But OK, I guess this is just how it is with 2D-arrays.

    I just thought there was a logical explanation like there is with string literals, i.e. that in
    Code:
    *test[] = {"hi", "mi"}
    , then test[0] points to the first character in the string literal "hi" etc..
    I think the difference here (if there is a difference here, which I'm not entirely convinced thiere is) is that in your test array the strings hi and mi live god knows where -- the values in the array are pointers to somewhere else, while in a 2D array there are no pointers involved anywhere (excepting the conversion of the name of the array to a pointer when used by itself). So your test array might store 0x403000 and 0x403003, while if you did
    Code:
    char test2[2][3] = {"hi", "mi"};
    your test2 array will store 'h', 'i', 0, 'm', 'i', 0 -- no pointers anywhere. (Now test2[1], by itself, is the name of an array, so it will be converted to the address of the beginning of the second "row" of your array.)

    EDIT: Or perhaps you're misunderstanding "second element". test2[1] is the second element of the test2 array -- which is to say, it is the second row/string/whatever you want to call it. It is not the second character stored, which is the second element of the first element of the array.
    Last edited by tabstop; 09-12-2009 at 02:10 PM.

  5. #50
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Niels_M View Post
    That is what I meant by "accessing" the array. But OK, I guess this is just how it is with 2D-arrays.
    Not sure what you mean by the above statement
    Quote Originally Posted by Niels_M View Post
    I just thought there was a logical explanation like there is with string literals, i.e. that in
    Code:
    *test[] = {"hi", "mi"}
    , then test[0] points to the first character in the string literal "hi" etc..
    Yep there is a logical explanation but there is a big difference here. test[] is an array of pointers to string literals, whose each element is a pointer to an array of characters. Alternatively each element of test can point to a single character but that is a huge waste of memory. It is like having an array of characters with a pointer to each of its element - utterly wasteful.

    A 2D array (or any multidimensional array) in C is really a 1D array. So even tho' asd[2][3] is a 2D array, it helps to think in terms of rows and columns but inside the machine it is laid out as one big row of storage cells.

  6. #51
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Niels_M View Post
    I just thought there was a logical explanation like there is with string literals, i.e. that in
    Code:
    *test[] = {"hi", "mi"}
    , then test[0] points to the first character in the string literal "hi" etc..
    The same is true for 2D arrays; asd[0] points to the first integer of the array asd[0]
    Code:
    asd[0] = &asd[0][0]; /* points to the number 1 */
    Last edited by itCbitC; 09-12-2009 at 02:40 PM.

  7. #52
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by tabstop View Post
    (Now test2[1], by itself, is the name of an array, so it will be converted to the address of the beginning of the second "row" of your array.)
    Ok, I think I understand it now. We have

    Code:
    char testarr[][25] ) {"thanks", "everybody"}
    Here (testarr+1) is the address of "everybody", and thus *(testarr+1) is the "name" of the array, and thus the address of the first element. Thus *(*(testarr+1)+1) gives us a 'v'.
    Last edited by Niels_M; 09-13-2009 at 12:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM