为什么main()也只能用于argc但不能用于argv? [英] Why main() works also only with argc but not argv?

查看:104
本文介绍了为什么main()也只能用于argc但不能用于argv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C程序的命令行参数感到困惑。

argc维护传递的参数计数,argv是这些参数的向量数组。但是当我这样做时:

I am confused in command line arguments to a C program a little bit.
The argc maintains the count of the arguments passed and argv is array of vector to these arguments.But when i do :

展开 | 选择 | Wrap | 行号

推荐答案

主要猜测接收(至少)2个参数。即使你可以使用较少的参数声明它,也可以使用参数个数和参数数组作为参数调用main函数。因此,如果''main''声明为

只有1个参数,则该参数接收(最多为演员)参数的数量。在第二个示例中,为argv分配了参数个数(命令名为+1)。所以* argv [0]在地址1处读取产生

段错误。
Main is guessed to receive (at least) 2 arguments. Even if you can declare it with less arguments, the main function is called with the number of arguments and the array of arguments as parameters. Thus if ''main'' is declared with
only 1 parameter this one receives (up to a cast) the number of arguments. In your second example, argv is assigned the number of arguments (+1 for the command name). So *argv[0] reads at address 1 which yields the
segfault.


对不起我没有得到你,你说至少有2个论点,还有什么其他的参数可以传递给main?how​​?
i am sorry i didnt get you,you said atleast 2 arguments,what other arguments can be passed to main?how?


你可以有第三个参数包含环境

变量;它是一个以NULL结尾的字符串数组。


int main(int argc,char ** argv,char ** env);


我从未见过任何程序使用它。也许是因为你需要在env中解析自己的字符串。每个字符串

的格式为VARNAME = VALUE。函数''getenv''更方便
检查

环境变量的值(甚至存在)。
You can have a third argument that contains environment
variables; it is a NULL terminated array of strings.

int main (int argc, char **argv, char **env);

I had never seen any program using it. Perhaps because
you have to parse yourself strings in ''env''. Each string
has the form VARNAME=VALUE. The function ''getenv'' is more
convenient to check the value (and even existence) of an
environment variable.


这篇关于为什么main()也只能用于argc但不能用于argv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆