# Thread: Multidimensional arrays

1. the way I see it, copying doesn't imply a change of types, and conversion does. also, linguistically, I'd guess that conversion suggests in-place conversion, but it doesn't really work like that in C... if you're only talking about temporary values, you might see it that way.

2. Right, but then how is int->int a conversion?

3. it's not, afaik.

4. Ok, so we're basically talking about specific wording in the standard, then - correct? If so, you'll have to forgive my ignorance - I'm a software engineer, not a language expert.

5. I'm just going by my understanding... I'm no expert either... I'm wrong fairly regularly.

6. Well that clears it up.

7. lol I walked into that one.

8. Hi

I have a final question (I've said that quite a few times, but hopefully I will mean it this time!).

We have

Code:
```int array[2][2] { {1,2}, {3,4} };
int (*ptr)[2];
ptr = array;```
The address "array" is the address if the first array in "array[5][11]". How is this stored in memory?

I mean, it is not like with string literals, since it is an initializer. So how does it work?

9. The array here has nothing to do with either 5 or 11. You have space for four integers, and that space has been filled with the integers 1, 2, 3, and 4. (Just like with the previous two-dimensional array, the values are stored in order across and then down: [0][0], then [0][1], then [1][0], then [1][1].)

10. Uf, the [5][11] was an error - it should of course have been [2][2].

But how can an array have an address as a whole? It must have one, since it is passed to ptr.

11. Originally Posted by Niels_M
Uf, the [5][11] was an error - it should of course have been [2][2].

But how can an array have an address as a whole? It must have one, since it is passed to ptr.
Every object has an address, which is where it is stored. An array is no different -- it has four constituent objects, yes, but the array itself lives somewhere and that is its address. So array and array[0] and array[0][0] share the same address, because they all start at the same place.

12. 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?

13. When you declare an array of any dimension, the compiler sets aside a block of contiguous bytes, and uses the array name as a reference to the first element. So for example:

Code:
```	size_t
data[ 3 ][ 4 ];```
The compiler allocates 3 * 4 * sizeof( size_t ) bytes on the stack, and since the array name is an reference to the first element, then ( ( size_t* )data ) == &data[ 0 ][ 0 ]. And because we're dealing with an array of arrays, ( ( size_t* )( data + 1 ) ) == &data[ 1 ][ 0 ]. Maybe this will make it more clear:

Code:
```#include <stdio.h>
#include <stdlib.h>

int main( void )
{
size_t
data[ 3 ][ 4 ] =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
},
size_in_bytes = sizeof( data ),
number_of_elements = size_in_bytes / sizeof( size_t ),
rows = size_in_bytes / sizeof( data[ 0 ] ),
columns = number_of_elements / rows,
row,
column;
printf( "size_in_bytes: %d \n", size_in_bytes );
printf( "number_of_elements: %d \n", number_of_elements );
printf( "rows: %d \n", rows );
printf( "columns: %d \n", columns );
printf( "address of data: %x \n", ( size_t* )data );
printf( "address of data + 1: %x \n", ( size_t* )( data + 1 ) );
for( row = 0; row < rows; ++row )
{
for( column = 0; column < columns; ++column )
{
printf
(
"<%x> data[ %d ][ %d ] = %d \n",
&data[ row ][ column ],
row,
column,
data[ row ][ column ]
);
}
}
return 0;
}```

14. I am still unclear about it. I am used to that data+1 is the address of element #2, and that *(data+1) is the value.

In this case, *(data+1) is the same address as data+1. I cannot see why.

15. Originally Posted by Niels_M
I am still unclear about it. I am used to that data+1 is the address of element #2, and that *(data+1) is the value.

In this case, *(data+1) is the same address as data+1. I cannot see why.
Right, well this is a two-dimensional array, so the second 'element' is an array, namely the one that starts at &data[ 1 ][ 0 ]. So then ( data + 2 ) == &data[ 2 ][ 0 ], and so on.

Popular pages Recent additions