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.
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, then test[0] points to the first character in the string literal "hi" etc..Code:*test[] = {"hi", "mi"}
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
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.)Code:char test2[2][3] = {"hi", "mi"};
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.
Not sure what you mean by the above statement
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.
Ok, I think I understand it now. We have
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'.Code:char testarr[][25] ) {"thanks", "everybody"}
Last edited by Niels_M; 09-13-2009 at 12:55 PM.