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]. 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.
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.template< size_t m, size_t n > void f( float (&a)[m][n] ) { ... }
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