Why does the char * argv [] statement work in the main arg, but not in the code body?

5

Because when I use the char *argv[] statement in int main(int argc,char *argv[]) it works, but when I try to use it inside the code body with char *argv[]; does it not work?

The error below is returned at the console when compiling.

error: array size missing in 'argv'
     char *argv[];
           ^

How could I use it inside the code body?

    
asked by anonymous 18.07.2016 / 21:12

2 answers

5

Because you need to tell the size of the array for the allocation to be made, or initialize the content so that the compiler can infer the size.

It works on main() because the allocation is done by the runtime language with data coming from the operating system, so the parameter declaration does not need to know the size, it only gets the data Declining for a pointer that does not have size, does not allocate it, so it does not need size.

    
18.07.2016 / 21:36
2
  

Why does the char *argv[] statement work in the main arg, but not in the code body?

Why ( Standard 6.7.6.3p7 ) the declaration of a parameter like "type array" will be set to "pointer to type" ... not needing the information about the number of elements.

That is, because in the case of parameters for functions, *argv[] is transformed into **argv .

int foo(char *arr[]) { /* ... */ }
int bar(char **arr) { /* ... */ }

foo () and bar () have completely equal signatures.

    
19.07.2016 / 11:08