Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Does C stardand allow gaps between the subarrays of arr[5][5], for example, arr[1][0] does not follow arr[0][4] directly? Commented Mar 3, 2014 at 11:01
  • @leeduhem No, I don't think it allows. The C standard states that array elements are contiguous in memory, and that they are stored in row-major order (last subscript varies fastest). See section 6.2.5.1., "Array subscripting". It follows from this that there can't be gaps in between. Commented Mar 3, 2014 at 12:22
  • It looks like this can guarantee defined behavior for expression like *(p+4), because if p = &arr[2][2], p+4 always points to arr[3][1]. Commented Mar 3, 2014 at 12:30
  • 1
    @leeduhem No, it doesn't guarantee, because p+4 goes out of bounds of the array it is pointing to. Yes, you know that after arr[2] there is another array, but even though it's contiguous, for whatever it's worth, it's another array, so you can't do that. This is a dark corner of C. Theoretically, it might not work, but I believe it always works. I guess it's just an inconsistency in the standard. Commented Mar 3, 2014 at 12:36
  • Agreed, this may just be an inconsistency. Commented Mar 3, 2014 at 12:39