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

Asked

Viewed 180 times

5

Why when I use the declaration char *argv[] in the int main(int argc,char *argv[]) works, but when I try to use it inside the body of the code with char *argv[]; it doesn’t work?

The error below is returned in the console when compiling.

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

How could I use it inside the body of the code?

  • This really has something to do with JNI?

  • Yes, I’m trying to implement also within the body of jint JNI_OnLoad @mustache

  • Good luck making JNI work :D

  • Vish, so you cut my excitement :D

  • 1

    For simple things it’s easy.

  • Just for the record, this is what I intended to put on JNI, Simple C example of Doing an HTTP POST and consuming the Response, to be executed while carrying the lib, I will search and who knows, with luck, very luck, I manage, give up, ever. =)

Show 1 more comment

2 answers

6


Because it needs to, or say the size of array so that the allocation is done, or initialize the content so that the compiler can infer the size.

Works in the main() because the allocation is made by Runtime of the language with data coming from the operating system, so the parameter declaration does not need to know the size, it only receives the data decaying to a pointer which has no size, does not allocate it, so does not need the size.

It would work if you had the size, or if you had the pointer there too.

3

Why the statement char *argv[] works in the main Arg, but not in the body of the code?

Because (Standard 6.7.6.3p7) parameter declaration as "type array" will be adjusted to "pointer to type" ... not requiring information on the number of items.

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.