关于argc和argv [英] about argc and argv

查看:88
本文介绍了关于argc和argv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


请用例子告诉我,* argv []如何指向

字符串的数字。

hi all,

please tell me with example, how the *argv[] point to the the no of
strings.

推荐答案

6月27日下午3:16,vicky< goelvi ... @ gmail.comwrote:
On Jun 27, 3:16 pm, vicky <goelvi...@gmail.comwrote:

大家好,


请用例子告诉我,* argv []如何指向

字符串的编号。
hi all,

please tell me with example, how the *argv[] point to the the no of
strings.



你的问题没有多大意义。你似乎要求一个

的例子,说明* argv []如何指向argv字符串的数量。嗯,它是
不是。


但是,给定一个main()函数原型

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

然后,在初始进入main()时,

argc将被设置为<的字符串数量

argv将指向,并且

argv将被设置为指向

单个字符串的指针数组


因此,

argv [0]将指向程序名称。字符串,无论如何,

argv [1]将指向第一个参数字符串,

argv [2]将指向第二个参数字符串,

等等,

argv [argc-1]指向最后一个参数字符串,

argv [argc]指向NULL指针


所以,

#include< stdio.h>

#include< stdlib.h>

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

{

if(argc< 0)

printf(" ;失败:argc是否定的,并且'不允许'\\ nn;);

else

{

if(argc = = 0)

printf("成功:argc为零,所以没有argv []字符串

传递给main()\ n");

else

{

int arg;

for(arg = 0; arg< argc; ++ arg)

if(argv [arg]!= NULL)

printf("成功:argv [%d]指向\"%s

\" \ n",arg,argv [arg]);

否则

printf(Huh ????:argv [%d]指向NULL \ n,arg);

}

if(argv [argc] == NULL)

printf(成功:最后一个argv []指向NULL \ n);

else

printf("失败:最后一个argv []指向某个东西,而不是NULL

\ n");

}

返回EXIT_SUCCESS;

}


将枚举通过argv传递的字符串参数


HTH

-

Lew

Your question doesn''t make much sense. You seem to be asking for an
example of how *argv[] points to the number of argv strings. Well, it
doesn''t.

However, given a main() function prototype of
int main(int argc, char *argv[]);
then, upon initial entry into main(),
argc will have been set to the count of the number of strings that
argv will point to, and
argv will have been set to point to an array of pointers to the
individual strings

Thus,
argv[0] will point to the "program name" string, what ever that is,
argv[1] will point to the first argument string,
argv[2] will point to the second argument string,
and so on, with
argv[argc-1] pointing to the last argument string, and
argv[argc] pointing at a NULL pointer

So,
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 0)
printf("Failure: argc is negative, and that''s not allowed\n");
else
{
if (argc == 0)
printf("Success: argc is zero, so there are no argv[] strings
passed to main()\n");
else
{
int arg;
for (arg = 0; arg < argc; ++arg)
if (argv[arg] != NULL)
printf("Success: argv[%d] points to \"%s
\"\n",arg,argv[arg]);
else
printf("Huh????: argv[%d] points to a NULL\n",arg);
}
if (argv[argc] == NULL)
printf("Success: the last argv[] points to NULL\n");
else
printf("Failure: the last argv[] points to something, not NULL
\n");
}
return EXIT_SUCCESS;
}

will enumerate the string parameters passed through argv

HTH
--
Lew




" vicky" ; < go ******* @ gmail.comwrote in message

news:11 ********************** @ z28g2000prd.googlegr oups.com ...

"vicky" <go*******@gmail.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...

大家好,


请举例告诉我,* argv如何[]指向

字符串的编号。
hi all,

please tell me with example, how the *argv[] point to the the no of
strings.



这是一个向用户回应争论的简单程序。


#include< stdio.h>


int main(int argc,char ** argv)

{

int i;


for(i = 0; i< argc; i ++)

printf(" ***% s *** \ n",argv [i]);

返回0;


}


如果你问argv数组是如何设置的,main()不是真正的条目

指向程序。有一些启动代码查询输入的

命令行界面,并设置main()到

读取的字符串。然而,它是非常特定于平台的,通常你永远不需要

了解它。


-

免费游戏和编程好东西。
http://www.personal.leeds。 ac.uk/~bgy1mm

Here''s a simple program to echo arguements to the user.

#include <stdio.h>

int main(int argc, char **argv)
{
int i;

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

}

If you are asking how the argv array is set up, main() isn''t the real entry
point to the program. There is some startup-code which queries the
command-line interface for the input, and sets up strings for main() to
read. However it is very platform-specific and normally you never need to
know about it.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


In< 11 ***************** *****@z28g2000prd.googlegroups .comvicky< go ******* @ gmail.comwrites:
In <11**********************@z28g2000prd.googlegroups .comvicky <go*******@gmail.comwrites:

请以示例告诉我,如何* argv []指向

字符串的数量。
please tell me with example, how the *argv[] point to the the no of
strings.



argv是一个字符串数组。

argc表示数组包含的条目数。


所以,例如,如果你想显示第一个字符串,你可以这样做:b $ b这样做:


printf("%s \ n",argv [0]);


(请记住,C数组从零开始编制索引。)


-

John Gordon A是Amy,他从楼梯上摔下来
go_S@panix.com B代表Basil,受熊袭击

- Edward Gorey,The Gashlycrumb Tinies

argv is an array of character strings.
argc indicates how many entries the array contains.

So, for example, if you wanted to display the first string, you could
do it like this:

printf("%s\n", argv[0]);

(Remember, C arrays are indexed starting at zero.)

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


这篇关于关于argc和argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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