By Oliver Charlesworth

2011-06-09 09:48:40 8 Comments

I imagine we all agree that it is considered idiomatic C to access a true multidimensional array by dereferencing a (possibly offset) pointer to its first element in a one-dimensional fashion, e.g.:

void clearBottomRightElement(int *array, int M, int N)
    array[M*N-1] = 0;  // Pretend the array is one-dimensional

int mtx[5][3];
clearBottomRightElement(&mtx[0][0], 5, 3);

However, the language-lawyer in me needs convincing that this is actually well-defined C! In particular:

  1. Does the standard guarantee that the compiler won't put padding in-between e.g. mtx[0][2] and mtx[1][0]?

  2. Normally, indexing off the end of an array (other than one-past the end) is undefined (C99, 6.5.6/8). So the following is clearly undefined:

    struct {
        int row[3];           // The object in question is an int[3]
        int other[10];
    } foo;
    int *p = &foo.row[7];     // ERROR: A crude attempt to get &foo.other[4];

    So by the same rule, one would expect the following to be undefined:

    int mtx[5][3];
    int (*row)[3] = &mtx[0];  // The object in question is still an int[3]
    int *p = &(*row)[7];      // Why is this any better?

    So why should this be defined?

    int mtx[5][3];
    int *p = &(&mtx[0][0])[7];

So what part of the C standard explicitly permits this? (Let's assume for the sake of discussion.)


Note that I have no doubt that this works fine in all compilers. What I'm querying is whether this is explicitly permitted by the standard.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] How to Sort Multi-dimensional Array by Value?

29 Answered Questions

[SOLVED] How to Flatten a Multidimensional Array?

12 Answered Questions

[SOLVED] Syntax for creating a two-dimensional array

22 Answered Questions

[SOLVED] PHP multidimensional array search by value

49 Answered Questions

[SOLVED] How can I create a two dimensional array in JavaScript?

9 Answered Questions

17 Answered Questions

2 Answered Questions

3 Answered Questions

[SOLVED] Does C99 guarantee that arrays are contiguous?

Sponsored Content