Something like this is simple enough to check (a) :
#include <stdio.h> int main (void) { int a[2][3][4][5]; // Ignore incorrect format specifiers for now. printf ("%d\n", sizeof(int)); printf ("%d\n", &(a[0][0][0][0])); printf ("%d\n", &(a[1][1][1][1])); printf ("%d\n", (int)&(a[1][1][1][1]) - (int)&(a[0][0][0][0]) + 1000); return 0; }
and the conclusion of this:
4 2665056 2665400 1344
Note the conversion of pointers to int values ββin this printf final. Without this, 1000 will scale as int * , indicating the wrong value.
So yes, in the bottom line, your reasoning is correct.
(a) This is not always the case, since some aspects of the C language may differ in different implementations (the behavior specified in the implementation) or in some way they want (undefined behavior).
Fortunately, array layout is specifically specified by the standard, in C11 6.5.2.1 Array subscripting :
2 / A postfix expression followed by an expression in square brackets [] is the indexed designation of an element of an array object. The definition of the index operator [] is that E1[E2] identical (*((E1)+(E2))) . Due to conversion rules that apply to the binary + operator, if E1 is an array object (equivalent to a pointer to the source element of an array object) and E2 is an integer, E1[E2] stands for E2-th element of E1 (counting from zero).
3 / The operators of subsequent indices denote an element of a multidimensional array object. If E is an n-dimensional array (n> = 2) with dimensions i * j * ... * k , then E (used as different from lvalue) is converted to a pointer to an (n - 1) -dimensional array with dimensions j * ... * k . If the unary * operator is applied to this pointer explicitly or implicitly as a result of the signature, the result is a reference (n - 1) -dimensional array, which itself is converted to a pointer if it is used as a value other than lvalue.This implies that the arrays are stored in lowercase order (the last index changes faster).
source share