int a[3][4] = {
1,2,3,4,
5,6,7,8,
9,10,11,12,
};
printf("%u %u %u \n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
int a[3][4] = { 1,2,3,4, 5,6,7,8, 9,10,11,12, }; printf(%u %u %u \n, a[0]+1, *(a[0]+1),
Share
Time for a crash course on arrays in C.
First of all, let’s fix the initializer for the array:
This defines a 3-element array of 4-element arrays of
int. The type of the expressionais “3-element array of 4-element arrays ofint“.Now for the headache-inducing part. Except when it’s the operand of the
sizeofor 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
aappears by itself in the code (such as in a statement likeprintf("%p", a);, its type is converted from “3-element array of 4-element array ofint” to “pointer to 4-element array ofint“, orint (*)[4]. Similarly, if the expressiona[i]appears in the code, its type is converted from “4-element array ofint” (int [4]) to “pointer toint” (int *). Ifaora[i]are operands of eithersizeofor&, 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 offsetielements 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 aboveHopefully, that should give you everything you need to figure out what the output should be. However, the
printfstatement should be written as