What does char * argv[] means?

一世执手 提交于 2019-12-03 08:17:11

问题


I'm new to C programming, I encountered a problem.
In case of complicated declarations i found this

int *daytab[13]; // daytab is an array of 13 pointers to int

which means daytab is the name of the array and the name of the array points to the first element of the array. The array name is not compatible with pointer manipulation like daytab++ etc (correct me if I'm wrong).

But I found this code written in Dennis Ritchie

main(int argc, char * argv[]) {
    while( --argc > 0 )                    
        printf("%s%s",*++argv,(argc>1) > " " : "");

    printf("\n");
    return 0;
}

How can they manipulate argv? Is it not the array name?


回答1:


The parameter char * argv[] decays to a pointer, char ** argv. You can equally well write the function signature for main() as:

int main(int argc, char ** argv)

You can do what you like with the pointer argv within main(), so argv++ for example just bumps argv to point at argv[1] rather than argv[0].

argv ---> argv[0] ---> "program"
          argv[1] ---> "arg1"
          argv[2] ---> "arg2"
           ...          ...
          argv[argc] == NULL



回答2:


When a program starts, it gets it's argument in the main function. That's why you ususally write.

int main(int argc, char *argv[])

This simply means that argv is a pointer to as many argument strings as indiciated by argc (== argument count). Since argv decays to char **argv you can also increase it, or you it otherwise like a pointer.

So if you want to print all arguments from the commandline:

int main(int argc, char *argv[])
{
   for(int i = 0; i < argc; i++)
       printf("%s\n", argv[i]);

   for(int i = 0; i < argc; i++)
       printf("%s\n", argv++);

    return 0;
}



回答3:


argv is an array of char*. Doing ++argv means accessing the next cell of the array. The * indicates we want the value of the cell, not the address.




回答4:


The declaration char *argv[] is an array (of undetermined size) of pointers to char, in other words an array of strings.

And all arrays decays to pointers, and so you can use an array as a pointer (just like you can use a pointer as an array). So *++argv first increases the "pointer" to point to the next entry in the array argv (which the first time in the loop will be the first command line argument) and dereferences that pointer.




回答5:


The parameter declaration looks similar to the declaration of an array but in fact (because it is a function parameter) it isn't one. The C FAQ explains that well.




回答6:


argc and argv are the parameters passed to the main function of the program.

argc in an integer holding the number of parameters, and argv is a pointer to a string array holding the actual parameters. Each element referenced by argv are separated by whitespace.




回答7:


What is done here is pointer arithmetic.

The pointer is not really changed.

The pointer's adress is used, incremented temporarily and then used for output.

Because the operator ++ is placed before the argv, the adress is first taken, then incremented, and then dereferenced to get the char* behind this adress.

But it is not the adress itself, which gets changed, just the offset, from where the printf will read.

Ok ?



来源:https://stackoverflow.com/questions/16666353/what-does-char-argv-means

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!