1. ## multidimention

hi all...

1. why this works:

Code:
```     int moo[2][MAX] = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};```

2. but this doesn't:

Code:
``` int moo[2][MAX];
moo = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};```

when compiling 2 i get:
error: syntax error before '{' token
which points the line of: "moo = {" from 2.

thanks...

2. { syntax is only valid when initializing an array at the same time you are declaring it.
Once it is declared , you have to use the complete syntax -

Code:
`moo[0][0] = 2;`

3. Originally Posted by toshog
hi all...

1. why this works:

Code:
```     int moo[2][MAX] = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};```

2. but this doesn't:

Code:
``` int moo[2][MAX];
moo = {
{2,4,34,43,23,0},
{2,4,34,43,23,0}
};```

when compiling 2 i get:
error: syntax error before '{' token
which points the line of: "moo = {" from 2.

thanks...
The reason for the second one being incorrect is because "moo" is the equivalent to
&moo[0][0] which is an adress not an element of the array, which is moo[row][column].

4. moo is equivalent to &moo[0], not &moo[0][0].

5. moo is the base address of the 2D array, which is address of moo[0][0], isn't it?

6. C only has one-dimensional arrays, technically speaking. multidimensional arrays are made by making arrays of arrays. since the rule is "the name of an array is converted to a pointer to its first element" or somesuch, only the outer array is converted to a pointer. so it's a pointer to an array of MAX ints, or int (*)[MAX].

7. So, what will be moo in case it would've been a 3D array like moo[3][3][3]? Would it be still &moo[0]?

8. yes, of type int (*)[3][3]. pointer to an array of three elements of an array of three elements of int.

9. Thanks. But I'm still a bit confused coz of this.
Code:
```int a[3][3]={1,2,3,4,5,6,7,8,9};
printf("%p	%p	%p",a[1],&a[1],*(&a[1]));```
How the hell are all of the three values coming out to be equal? I know that a[1] is the address of 1st 1D array, but what about the rest of them?

10. the compiler probably uses a representation for "pointer to array" types that points to the first element of the array. so the only difference would be types.

a[1] would be an int[3], which gets converted to an int *.
&a[1] would be an int(*)[3], because the array-to-pointer conversion isn't applied when an array type is the operand of the & or sizeof operators. &a[1] and a[1] begin at the same place then, and if the representation of a pointer to an array is the starting place of the array, then they would both have the same "value".
*(&a[1]) is the same as the first one. a[1] is an int[3], & gives an int(*)[3], * gives an int[3], and that gets converted to an int *.

actually, that program has undefined behavior because %p is for void*, not any pointer type, although most compilers use the same representation for their pointer types, so it usually works.

use:

int a[3][3]={1,2,3,4,5,6,7,8,9};
printf("%p %p %p", (void*)a[1], (void*)&a[1], (void*)*(&a[1]));

11. Originally Posted by robwhit
*(&a[1]) is the same as the first one.
Stupid me.

12. fixed the explanation again.

13. This one makes it more clear. I also tried a[1]+1 and &a[1]+1 and understood the difference. Thanks.