Are C multidimensional arrays contiguous without holes?
According to 6.2.5 Types
p20:
An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type. The element type shall be complete whenever the array type is specified. ...
Therefore all array types, multidimensional or not, are contiguously allocated.
Yes, it can be obtained by induction. (Just to add, as a suggestion, if that helps, try to think of multi-dimensional arrays as array of arrays.)
For example, consider an array like a[3][3]
.
So,
a[0][0]
,a[0][1]
anda[0][2]
are elements ofa[0]
and they will be contiguous.Next,
a[0]
anda[1]
are elements ofa
, so it will be contiguous
an so on.
Taken together, a[0][2]
and a[1][0]
will be residing next to each other, thereby continuing the contiguity.
For better visual representation, see the below illustration.
The array, say int arr[4][5]
, has four rows, a[0]
,a[1]
, a[2]
and a[3]
and they are contiguous.
Now each of those rows have five columns, like a[n][0]
, a[n][1]
, a[n][2]
, a[n][3]
, a[n][4]
and they are contiguous.
So, the all the elements (and elements of elements) of the array are contiguous.
C does not explicitly have multi-dimentional arrays, C have array of arrays, and arrays in C are contiguously represented in memory. Hence all arrays in C are contiguous.
Yes they are contiguous. I would say the fact "an array" (i.e. singular) is contiguous infers that a multi-dimensional one is. Each array within it must be contiguous and the outer array must be a contiguous collection of those arrays...