C语言 如何从C中的main函数打印argv参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35860794/
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
How to print argv arguments from main function in C?
提问by
So I just learnt that we can give two arguments into main function namely "argc" and "argv. However I am not able to understand what argv is in this:
int main(int argc, char* argv[]);
所以我刚刚了解到我们可以给主函数提供两个参数,即“argc”和“argv。但是我无法理解 argv 是什么:
int main(int argc, char* argv[]);
Is argv an array of characters? or is it an array of pointers pointing to characters? Either way I am looking for a way to print out the arguments that the user passes to this program. This is the code the I wrote, but it's not printing the argv's so to speak. What's wrong in it? I guess it's my understanding of argv that's making this code incorrect.
argv 是字符数组吗?还是指向字符的指针数组?无论哪种方式,我都在寻找一种方法来打印出用户传递给该程序的参数。这是我写的代码,但它没有打印 argv 可以这么说。它有什么问题?我想这是我对 argv 的理解导致此代码不正确。
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("%d\n",argc);
for(i=0;i<argc-1;i++)
{
printf("%s",*argv[i]);
}
return 0;
}
After the suggestions that I got from answers, I corrected my code as follows.
在我从答案中得到建议后,我按如下方式更正了我的代码。
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("%d\n",argc);
for(i=1;i<argc;i++)
{
printf("%s",argv[i]);
}
return 0;
}
And I am using Ubuntu Linux in VMWare on my windows 8.1 pc. This is the output that I am getting. It's just printing argc and after that nothing. What's the problem? Is it the way I am compiling it or something in Linux terminal?
我在 Windows 8.1 pc 上的 VMWare 中使用 Ubuntu Linux。这是我得到的输出。它只是打印 argc,之后什么也没有。有什么问题?是我编译它的方式还是Linux终端中的其他东西?
In the above figure, I want the numbers 2,4,5,3 to be printed again, but they are not getting printed.
在上图中,我希望再次打印数字 2、4、5、3,但它们没有被打印出来。
Thanks.
谢谢。
采纳答案by 3442
Let's explain out things step by step. First of all, when you invoke your program by calling something like...
让我们一步一步地解释事情。首先,当您通过调用类似...
./my-program arg0 arg1 arg2
You're passing it a series of three arguments, right? And each argument is a string, right? Now, the mainfunction, can have one of two prototypes, as specified by the C standard...
你传递了一系列三个参数,对吧?每个参数都是一个字符串,对吗?现在,该main函数可以具有 C 标准指定的两个原型之一...
int main(); // Let's not worry about this for now
int main(int argc, char **argv);
The idea is that mainis able to handle the arguments that you provided. argcprovides the number of arguments. If you noticed, while passing three arguments, argcis 4! This happens because a first argument is passed before all others, ./my-program, and lets your program recognize itself.
这个想法是main能够处理您提供的参数。argc提供参数的数量。如果您注意到,在传递三个参数时,argc是 4!发生这种情况是因为第一个参数在所有其他参数之前传递./my-program,并让您的程序识别自身。
Now, what does char **argvmean? Something of the form X*is a pointer to X, right? So, char *is a pointer to char, and char **is a pointer to pointer to char. In C, a string is simply a zero-terminated array of char, and an array can be "degraded" into a pointer. This means that argvis an array of strings, the first of which, argv[0], is the program's name.
现在,是什么char **argv意思?某种形式X*是指向 的指针X,对吗?所以,char *是指向 的指针char,char **是指向 的指针char。在 C 中,字符串只是一个以零结尾的数组char,数组可以“降级”为指针。这意味着它argv是一个字符串数组,其中第一个argv[0]是程序的名称。
Now, the C standard allows you to write any "compatible" prototype for main. For instance, you can write any of these...
现在,C 标准允许您为main. 例如,您可以编写任何这些...
int main(int argc, const char *const argv[]);
int main(int argc, const char *argv[])
int main(int argc, const char **argv);
int main(int argc, const char *const *const argv);
You don't need to understand what they all mean now, just that argvis an array of strings, and that you should nevermodify strings as the original mainprototype appears to trust you onto. Now, knowing that arguments start at argv[1], your code...
您现在不需要理解它们的全部含义,它只是argv一个字符串数组,并且您永远不应该修改字符串,因为原始main原型似乎信任您。现在,知道参数从 开始argv[1],您的代码...
for(i=0;i<argc-1;i++)
Means: "For each iin the range of 0 through argc - 1".
意思是:“对于i0 到 0 范围内的每个argc - 1”。
printf("%s",*argv[i]);
Means: "Print the first character of the ith element of argv". Why would this be wrong? First of all, you're printing a char, and telling printfthat it is a string. This has undefined behavior. Then, you're iterating over the first ith elements of argv. This means that the first, "non-argument" element will be included in the mix, and the last argument won't be. To solve it, write somethinge like...
意思是:“打印”的第一个i元素的第一个字符argv。为什么这会是错误的?首先,您正在打印 a char,并告诉printf它是一个字符串。这有未定义的行为。然后,您将遍历 的第一个ith 元素argv。这意味着第一个“非参数”元素将包含在混合中,而最后一个参数则不会。要解决它,请写一些类似...
for(i = 1; i < argc; i++)
Means: "For each iin the range from 1up to argc".
意思是:“对于i从1最多到argc”范围内的每个。
printf("%s", argv[i]);
Means: "Print the ith element of argvonto stdout.
意思是:“打印i的个元素argv上stdout。
回答by haccks
Is
argvan array of characters? or is it an array of pointers pointing to characters?
是
argv字符数组吗?还是指向字符的指针数组?
argvis a pointer to pointer to char. When a list of arguments are passed through command line then an array of charpointers are created and each of these pointers points to each of these arguments, stored in the form of strings, along with the program name. argvpoints to the first pointer of this char *array. Therefore, argv[i]is a pointer to char.
argv是指向 的指针char。当参数列表通过命令行传递时,就会char创建一个指针数组,每个指针都指向这些参数中的每一个,以字符串的形式存储,以及程序名称。argv指向这个char *数组的第一个指针。因此,argv[i]是指向 的指针char。
+--------------------------+
+----+ +--> | argument 1 (program name)|
argv[0]+----> | | | +--------------------------+
| +----+
+----| +--------------------------+
| +-------> | argument 2 |
| | +--------------------------+
+----+
| +----+ +--------------------------+
| | +--> | argument 3 |
+----+ +--------------------------+
"char *" array
You need to change
你需要改变
printf("%s",*argv[i]);
to
到
printf("%s",argv[i]);
*argv[i]is of type char. %sexpects a type of char *.
*argv[i]是 类型char。%s期望一种char *.
回答by MikeCAT
char *argv[]is pointer to pointer to charbecause arrays in function arguments are automatically converted to pointer pointing at elements of the array.
char *argv[]是指向指针的指针,char因为函数参数中的数组会自动转换为指向数组元素的指针。
You invoked undefined behaviorby passing data having wrong type to printf(): %sexpects char*, but you passed char(converted to intfor variable number arguments).
您通过将具有错误类型的数据传递给: expects来调用未定义的行为,但是您传递了(转换为变量数参数)。printf()%schar*charint
Remove the extra *o dereference the pointer.
删除额外的*o 取消引用指针。
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("%d\n",argc);
for(i=0;i<argc-1;i++)
{
printf("%s",argv[i]);
}
return 0;
}
Also you may want i<argcinstead of i<argc-1. Why not print the last argument?
你也可能想要i<argc代替i<argc-1. 为什么不打印最后一个参数?
回答by Ruslan
In your edited question you have fixed your main problem. Now notice that your prompt looks like
在您编辑的问题中,您已经解决了主要问题。现在注意你的提示看起来像
./mygrep245YourName@ubuntu:~/test$
Here ./mygrep245is your output after you printed "5\n". You forgot to print line feeds, thus all the output and the subsequent shell output has been concatenated as it is. And the last argument 3wasn't printed because you put i<argc-1instead of i<argcor i<=argc-1in your loop condition.
这./mygrep245是打印后的输出"5\n"。您忘记打印换行符,因此所有输出和随后的 shell 输出已按原样连接。并且最后一个参数3没有打印出来,因为你在循环条件中放置了i<argc-1而不是i<argc或i<=argc-1。
回答by Rahul Tripathi
You can refer the document:
您可以参考文档:
As you can see, main now has arguments. The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program. The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings. Each string is one of the arguments that was passed to the program.
如您所见, main 现在有参数。变量 argc 的名称代表“参数计数”;argc 包含传递给程序的参数数量。变量 argv 的名称代表“参数向量”。向量是一维数组,argv 是一维字符串数组。每个字符串都是传递给程序的参数之一。
If you need to print it simply remove the * and try this:
如果您需要打印它,只需删除 * 并尝试以下操作:
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
printf("%d\n",argc);
for(i=0;i<argc-1;i++)
{
printf("%s",argv[i]); //remove * from here
}
return 0;
}
回答by grek40
Regarding
关于
Is argv an array of characters? or is it an array of pointers pointing to characters?
argv 是字符数组吗?还是指向字符的指针数组?
argvis an array of c-strings (where each c-string is a pointer to characters). Consider the following command line:
argv是一个 c 字符串数组(其中每个 c 字符串是一个指向字符的指针)。考虑以下命令行:
$ your-executable arg1 "arg2 with space"
It would result in an array of strings with the following content (pseudo code)
它会产生一个包含以下内容的字符串数组(伪代码)
argv[0] EQUALS "your-executable"
argv[1] EQUALS "arg1"
argv[2] EQUALS "arg2 with space"
As others mentioned, remove the * from printf("%s", *argv[i]);and fix your loop condition if you want to print all arguments
正如其他人提到的,printf("%s", *argv[i]);如果要打印所有参数,请删除 * from并修复循环条件


