简体   繁体   English

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

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

If I have this: 如果我有这个:

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

In the body, you can sometimes find programs using argv[1] . 在正文中,您有时可以使用argv[1]找到程序。

When do we use argv[1] over argv[0] ? 我们什么时候在argv[0]使用argv[1] argv[0] Is it only when we just want to read the second argument in the command line? 是否仅在我们想要在命令行中读取第二个参数时?

By convention , argv[0] is the current program's name (or path), and argv[1] through argv[argc - 1] are the command-line arguments that the user provides. 按照惯例argv[0]当前程序的名称 (或路径), argv[1]argv[argc - 1]是用户提供的命令行参数

However, this doesn't have to be true -- programs can OS-specific functions to bypass this requirement, and this happens often enough that you should be aware of it. 然而,这并不一定是真实的-程序可以与操作系统相关的功能来绕过这一要求,而这种情况往往不够,你应该意识到这一点。 (I'm not sure if there's much you can do even if you're aware of it, though...) (即使你知道它,我也不确定你能做多少事情......)

Example: 例:

gcc -O3 -o temp.o "My file.c"

would (should) produce the following arguments: 会(应该)产生以下参数:

argc: 5
argv: ["gcc", "-O3", "-o", "temp.o", "My file.c"]

So saying argv[0] would refer to gcc , not to -O3 . 所以说argv[0]会引用gcc而不是 -O3

argv is an array of pointers, and each pointer in this array stores one argument from command line. argv是一个指针数组,此数组中的每个指针都存储一个来自命令行的参数。 So argv[0] is the first argument (that is the executable/program itself), argv[1] is the second argument, and so on! 所以argv[0]是第一个参数(即可执行文件/程序本身), argv[1]是第二个参数,依此类推!

The total number of arguments is determined by argc . 参数总数由argc确定。

Let's suppose your C++ executable file is: 假设您的C ++可执行文件是:

/home/user/program (or C:\\program.exe in Windows) /home/user/program (或Windows中的C:\\program.exe

if you execute: 如果你执行:

./home/user/program 1 2 (or C:\\program.exe 1 2 in Windows) ./home/user/program 1 2 (或Windows中的C:\\program.exe 1 2

argv[0] = /home/user/program ( C:\\program.exe ) argv[0] = /home/user/programC:\\program.exe
argv[1] = 1
argv[2] = 2

That is because: 那是因为:

  • argv[0] is the path of the executable file argv[0]是可执行文件的路径
  • argv[1] is the 1st argument argv[1]是第一个参数

Edit: 编辑:

Now I see that argv[0] isn't necessarily the path of the executable file. 现在我看到argv[0]不一定是可执行文件的路径。
Read the following SO question: Is args[0] guaranteed to be the path of execution? 阅读以下SO问题: args [0]是否保证是执行路径?

argv [0]是程序的执行路径,argv [1]是程序的第一个参数

Yes, that's mostly it, argv[1] is the second command line parameter. 是的,主要是它, argv[1]是第二个命令行参数。 The first command line parameter is the name of the program itself. 第一个命令行参数是程序本身的名称。

Alternatively, to avoid the semantic mess that this answer originally had, and the comments from others, it might make sense to call argv[0] the zeroth parameter, so that argv[1] would now be the "first" of the user supplied values. 或者,为了避免此答案最初具有的语义混乱以及其他人的评论,将argv [0]调用为第0个参数可能是有意义的,因此argv[1]现在将成为用户提供的“第一个”值。

In any event, this comes from the exec() family of functions, eg execl which has usage: 无论如何,这来自exec()函数系列,例如execl ,它具有以下用途:

 int execl(const char *path, const char *arg0, ... /*, (char *)0 */);

In the (Unix) shell when you type in a command, if necessary the shell first resolves the command name (using $PATH ) to find the real absolute path. 在(Unix)shell中键入命令时,如果需要,shell首先解析命令名(使用$PATH )以查找实际的绝对路径。 The (absolute or relative) path is supplied for path , and the command as originally typed-in is supplied as arg0 , eventually becoming argv[0] in your program. path提供(绝对或相对)路径,并且最初输入的命令以arg0形式提供,最终在程序中变为argv[0]

The remaining command line parameters then end up as argv[1] , etc. 其余的命令行参数最终为argv[1]等。

简短回答是,数组包含传递给程序的所有选项。

as argv[0] is filepath of the program itself. 因为argv [0]是程序本身的文件路径。 Extra command line parameters are in further indexes, argv[1],argv[2].. You can read more here : http://www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3/CommandLineArguments.html 额外的命令行参数在其他索引中,argv [1],argv [2] ..您可以在这里阅读更多内容: http//www.site.uottawa.ca/~lucia/courses/2131-05/labs/Lab3 /CommandLineArguments.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM