News for hackers. Stuff that matters.
Re:Are pointers and arrays equivalent in C? (2009) by Animats on Saturday March 21, 2015 @03:44PM (#9243957)
You can actually pass an array as an array in C99:

    #include <stdlib.h>

    void f(size_t m, size_t n, float a[m][n])
    {   for (size_t i = 0; i < m; i++) {
            for (size_t j=0; j < n; j++) {
                a[i][j] = 0;
            }
        }
    }

    int main(int argc, char* argv[]) {
        float a[10][20];
        f(10,20,a);
    }

This "conformant array" feature was supported in C99. GCC supports it. Microsoft didn't like it, though, and didn't implement it, so nobody uses it, and it was made optional in later versions of the C standard.

It doesn't do what it looks like it does. The last subscript is totally ignored by the compiler. For 1D arrays, it's not only ignored, the object is still passed as a pointer to the element type.

I once proposed making arrays in C size-safe by taking conformant arrays seriously and adding some features to make them more useful.[1] After much discussion, the idea was considered sound, but not popular enough. So buffer overflows in C continue.

I have great hopes for Rust.

[1] http://animats.com/papers/languages/safearraysforc43.pdf

Re:Are pointers and arrays equivalent in C? (2009) by Peaker on Saturday March 21, 2015 @04:06PM (#9244026)
That's ordinary C89 behavior, not a C99 feature, and I'm pretty sure Microsoft supports it too. The C99 feature here is VLA (variable-length array), arrays whose length is a runtime value.

If you change m and n to be constants, it works with C89.

The rule is simple (yet confusing!): In a function prototype, if the parameter (top-level) type is an array, it is de-sugared to be an element-pointer instead.

Thus, the code you wrote with constants instead:

    #define M 10
    #define N 20
    void f(size_t m, size_t n, float a[M][N])
De-sugars, as per C89 ordinary rules, to:

    void f(size_t m, size_t n, float (*a)[N])
And it is an ordinary array degradation to a pointer, except instead of pointing to a float, it happens to point at a float[N].
Re:Are pointers and arrays equivalent in C? (2009) by Animats on Saturday March 21, 2015 @04:22PM (#9244077)
Variable length arrays are a different feature. They are local temporary arrays of variable size:

    int f(size_t n) {
        int temp[n]; /* local array of size n */
    ...
    }
Someone once used that feature in the Linux kernel to get temp space for a string. It was taken out, because those arrays went on the stack and thus allowed large kernel stack growth based on data from outside the kernel.

Nobody uses that feature either.

Re:Are pointers and arrays equivalent in C? (2009) by Peaker on Saturday March 21, 2015 @04:47PM (#9244156)
EDIT: I now see what you mean, sorry for misunderstanding.

The feature sounds useful, but the reason I misunderstood you is that you used the same syntax that represents array-to-ptr degradation, which made me think you were confused :-) I'm not sure how you would make this properly backwards compatible with C. You'd need to use a different syntax for such a major new feature.

Kept here for posterity:

You used variable length arrays in conjunction with ordinary array-of-array notation that (in function parameter list context) degrades to ptr-to-array.

Here is the output of gcc on your code, with -Wvla (which warns about use of vla):

  gcc -c testvla.c -o /dev/null -Wvla -std=c99
  testvla.c:3:1: warning: variable length array ‘a’ is used [-Wvla]
   void f(size_t m, size_t n, float a[m][n])
   ^
  testvla.c:3:1: warning: variable length array ‘a’ is used [-Wvla]
The VLA is why your code is C99. If you use a[10][20] in the function prototype, it is ordinary C89, and if Microsoft wrote a C89 conformant compiler, it works on Microsoft's compiler as well.
Re:Are pointers and arrays equivalent in C? (2009) by MaulingMonkey on Saturday March 21, 2015 @05:50PM (#9244316)
Safe C++ equivalent which I've used on Microsoft's compilers:

template< size_t m, size_t n > void f( float (&a)[m][n] ) { ... }