PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, December 11, 2022

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

 December 11, 2022     c, gcc, syntax     No comments   

Issue

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?


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



Answered By - Thomas Giesel
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing