Time for a crash course on arrays in C.
First of all, let’s fix the initializer for the array:
int a[3][4] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
This defines a 3-element array of 4-element arrays of int. The type of the expression a is “3-element array of 4-element arrays of int“.
Now for the headache-inducing part. Except when it’s the operand of the sizeof or unary & operators, or if it’s a string literal being used to initialize another array in a declaration, an expression of array type will have its type implicitly converted (“decay”) to a pointer type.
If the expression a appears by itself in the code (such as in a statement like printf("%p", a);, its type is converted from “3-element array of 4-element array of int” to “pointer to 4-element array of int“, or int (*)[4]. Similarly, if the expression a[i] appears in the code, its type is converted from “4-element array of int” (int [4]) to “pointer to int” (int *). If a or a[i] are operands of either sizeof or &, however, the conversion doesn’t happen.
In a similar vein, array subscripting is done through pointer arithmetic: the expression a[i] is interpreted as though it were written *(a+i). You offset i elements from the base of the array and dereference the result. Thus, a[0] is the same as *(a + 0), which is the same as *a. a[i][j] is the same as writing *(*(a + i) + j).
Here’s a table summarizing all of the above:
Expression Type Decays To Resulting Value
---------- ---- --------- -----
a int [3][4] int (*)[4] Address of the first element of the array
&a int (*)[3][4] n/a Same as above, but type is different
*a int [4] int * Same as above, but type is different
a[0] int [4] int * Same as above
*(a+0) int [4] int * Same as above
a[i] int [4] int * Address of the first element of the i'th subarray
*(a+i) int [4] int * Same as above
&a[i] int (*)[4] n/a Same as above, but type is different
*a[i] int n/a Value of the 0'th element of the i'th subarray
a[i][j] int Value of the j'th element of the i'th subarray
*(a[i]+j) int Same as above
*(*(a+i)+j) int Same as above
Hopefully, that should give you everything you need to figure out what the output should be. However, the printf statement should be written as
printf("%p %d %d\n", (void *) a[0]+1, *(a[0]+1), *(*(a+0)+1));
1
solved what is the output? Please explain, considering i am a novice in c [closed]