C语言 char * argv[] 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16666353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:24:53  来源:igfitidea点击:

What does char * argv[] means?

carrayspointerskernighan-and-ritchie

提问by akash

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

我是 C 编程的新手,遇到了一个问题。
在复杂声明的情况下,我发现了这一点

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

which means daytabis 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).

这意味着daytab是数组的名称,数组的名称指向数组的第一个元素。数组名称与指针操作daytab++等不兼容(如果我错了,请纠正我)。

But I found this code written in Dennis Ritchie

但是我发现这段代码是用 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?

他们怎么能操纵argv?不是数组名吗?

回答by Paul R

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

参数char * argv[]衰减到一个指针,char ** argv。您同样可以将函数签名编写main()为:

int main(int argc, char ** argv)

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

你可以做你喜欢用指针argvmain(),这样argv++的例子只是颠簸argv到点argv[1],而不是argv[0]

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

回答by ibi0tux

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

argv是一个数组char*。做++argv意味着访问数组的下一个单元格。该*指示我们想要的细胞,而不是地址的值。

回答by Devolus

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.

这只是意味着 argv 是一个指针,指向与 argc(== 参数计数)所指示的参数字符串一样多。由于 argv 衰减为 char **argv 您也可以增加它,或者像指针一样增加它。

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;
}

回答by Jens Gustedt

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 FAQexplains that well.

参数声明看起来类似于数组的声明,但实际上(因为它是一个函数参数)它不是一个。该Ç常见问题解释说,好。

回答by Some programmer dude

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

声明char *argv[]是一个指向 的指针数组(大小不确定),char换句话说,是一个字符串数组。

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 *++argvfirst 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.

并且所有数组都衰减为指针,因此您可以将数组用作指针(就像您可以将指针用作数组一样)。因此,*++argv首先增加“指针”以指向数组中的下一个条目argv(循环中的第一次将是第一个命令行参数)并取消引用该指针。

回答by AcId

argcand argvare the parameters passed to the main function of the program.

argcargv是传递给程序主函数的参数。

argcin an integer holding the number of parameters, and argvis a pointer to a string array holding the actual parameters. Each element referenced by argvare separated by whitespace.

argc在一个保存参数数量的整数中,并且argv是一个指向保存实际参数的字符串数组的指针。引用的每个元素argv都由空格分隔。

回答by icbytes

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.

因为运算符 ++ 放在 argv 之前,所以先取地址,然后递增,然后取消引用以获取此地址后面的 char*。

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

但改变的不是地址本身,而是 printf 将读取的偏移量。

Ok ?

好 ?