Does C99 guarantee that arrays are contiguous ?

Posted by kriss on Stack Overflow See other posts from Stack Overflow or by kriss
Published on 2010-05-14T09:13:29Z Indexed on 2010/05/14 9:24 UTC
Read the original article Hit count: 338

Filed under:
|
|
|

Following an hot comment thread in another question, I came to debate of what is and what is not defined in C99 standard about C arrays.

Basically when I define a 2D array like int a[5][5], does the standard C99 garantee or not that it will be a contiguous block of ints, can I cast it to (int *)a and be sure I will have a valid 1D array of 25 ints.

As I understand the standard the above property is implicit in the sizeof definition and in pointer arithmetic, but others seems to disagree and says casting to (int*) the above structure give an undefined behavior (even if they agree that all existing implementations actually allocate contiguous values).

More specifically, if we think an implementation that would instrument arrays to check array boundaries for all dimensions and return some kind of error when accessing 1D array, or does not give correct access to elements above 1st row. Could such implementation be standard compilant ? And in this case what parts of the C99 standard are relevant.

© Stack Overflow or respective owner

Related posts about c

    Related posts about c99