Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can I write an expression in an array declaration of a function argument in C?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading