3 ms·
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 whos
by Peaker 12y ago
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].
- Animats 12y agoVariable 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.
- Peaker 12y agoEDIT: 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.