I have initialized a 2D array like a 1D array:
int a[2][3] = {1,2,3,4,5}
How are the values stored in this array?
From the definition of how an access to a 2D-array like a[1][2] is interpreted, "It follows from this that arrays are stored in row-major order" (cf, for example, this online C standard comitee draft / array subscripting).
This means that for an array int a[ROWS][COLUMNS] for an access a[r][c] the offset in terms of int values is calculated like (r*COLUMNS + c).
So for an array int a[2][3], an access a[0][1] has offset 0*3 + 1 = 1, and an access a[1][0] has an offset 1*3 + 0 = 3. That said, a[0][3] might lead to offset 3, while a[1][0] for sure leads to 3. I wrote "might" because I think that accessing an array int a[2][3] with a[0][3] is undefined behaviour, as the range of the last subscript is 0..2. So according to 6.5.6 (8), expression a[0][3] is addressing the sub-array a[0] out of its bounds as argued, for example, here.
Now to the thing of how int a[2][3] = {1,2,3,4,5} is interpreted. This statement is initialization as defined in section 6.7.9 of this online C standard comitee draft, and paragraphs (20) to (26) describe the things needed here:
(20) If the aggregate or union contains elements or members that are aggregates or unions, these rules apply recursively to the subaggregates or contained unions. If the initializer of a subaggregate or contained union begins with a left brace, the initializers enclosed by that brace and its matching right brace initialize the elements or members of the subaggregate or the contained union. Otherwise, only enough initializers from the list are taken to account for the elements or members of the subaggregate or the first member of the contained union; any remaining initializers are left to initialize the next element or member of the aggregate of which the current subaggregate or contained union is a part.
(21) If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
26 EXAMPLE
(3) The declaration
int y[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 }, };is a definition with a fully bracketed initialization:
1,3, and5initialize the first row ofy(the array objecty[0]), namelyy[0][0],y[0][1], andy[0][2]. Likewise the next two lines initializey[1]andy[2]. The initializer ends early, soy[3]is initialized with zeros. Precisely the same effect could have been achieved byint y[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };The initializer for
y[0]does not begin with a left brace, so three items from the list are used. Likewise the next three are taken successively fory[1]andy[2].