Home > Software design >  Why can I write an expression in an array declaration of a function argument in C?
Why can I write an expression in an array declaration of a function argument in C?

Time:10-31

Why does the following code compile without warnings or errors?

#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
__attribute__((__const__))
#endif
int len(char** array) {
    int i;
    for (i = 0; array[i]; i  );
    return i;
}
void inner_len(char** array, int out[len(array)]) {
    int i;
    for (i = 0; i < len(array); i  ) {
        out[i] = strlen(array[i]);
    }
}
int main() {
    char* array[] = {
        "hello",
        "world",
        NULL
    };
    int ilen[1];
    inner_len(array, ilen);
}

Especially the function declaration of inner_len is what confuses me the most about this. I have seen gcc provide warnings for wrong array lengths. Is the expression in the function declaration ignored?

CodePudding user response:

What you did here is to declare a variable length array. If you compile it as ISO C89, you get:

$ gcc --std c89 -pedantic -Wall bla.c
bla.c:9:1: warning: ISO C90 forbids variable length array ‘out’ [-Wvla]
    9 | void inner_len(char** array, int out[len(array)]) {
      | ^~~~

But as C99 there is no warning, since ISO C99 added variable length arrays, refer to, e.g., https://en.wikipedia.org/wiki/Variable-length_array#C99.

Note that out is still an ordinary pointer, as usually in C when you pass an array as argument to a function. And there is also no compile time length check here, since the compiler does run/evaluate the code in the function len(), other than when you'd used a literal like int out[2].

  • Related