理解char ** argv [英] Understanding char **argv

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

问题描述

大家好,


原谅这个有点简单的问题,我相信这会是,但我有点b / b
有一些问题需要了解:char ** argv看起来像。对于

实例,我通过反复试验知道如果我这样做:


#include< stdio.h>

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

{

int l;

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

{

printf(" Pointer:%p \tValue:%s \ n",argv ++,* argv); < br $>
}


返回0;

}


我调用上面的:


../foo一二三


然后我会看到:

0x10202 foo
0x10204一个

0x10208两个
ox1020c三个


已列出,但我原本预计需要写:


*(*(argv))


所以取消引用* argv指向的指针。或者有

我错过了这一点?


谢谢。


凯文

Hello all,

Forgive the somewhat simple question I am sure this will be, but I am
having some problem understanding what: char **argv looks like. For
instance, i know through trial and error that if I do this:

#include <stdio.h>

int main(int argc, char *argv[])
{
int l;
for( l=0; l<argc; l++ )
{
printf("Pointer: %p\tValue: %s\n", argv++, *argv);
}

return 0;
}

And i invoke the above as:

../foo one two three

Then I will see:
0x10202 foo
0x10204 one
0x10208 two
ox1020c three

Listed, but I would have expected to have needed to write:

*(*(argv))

So as to dereference what the pointer that *argv pointed to. Or have
I missed the point?

Thanks.

Kevin

推荐答案

ke ********** **@googlemail.com 说:

大家好,


原谅我这个有点简单的问题确定这会是,但我有一些问题需要了解:char ** argv看起来像。
Hello all,

Forgive the somewhat simple question I am sure this will be, but I am
having some problem understanding what: char **argv looks like.



argv是指向char指针数组中第一个元素的指针,

其中数组中的每个指针指向第一个元素字符串中的字符。

argv is a pointer to the first element in an array of pointers to char,
where each pointer in the array points to the first character in a string.


对于

实例,我通过反复试验知道如果我这样做:


#include< stdio.h>


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

{

int l;

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

{

printf(" Pointer) :%p \tValue:%s \ n",argv ++,* argv);
For
instance, i know through trial and error that if I do this:

#include <stdio.h>

int main(int argc, char *argv[])
{
int l;
for( l=0; l<argc; l++ )
{
printf("Pointer: %p\tValue: %s\n", argv++, *argv);



用这两行替换该行:


printf(" Pointer:%p \tValue:%s \\ \\ n",(void *)argv,* argv);

++ argv;


< snip>

Replace that line with these two:

printf("Pointer: %p\tValue: %s\n", (void *)argv, *argv);
++argv;

<snip>


已列出,但我原本预计需要写:


*(*(argv))
Listed, but I would have expected to have needed to write:

*(*(argv))



这相当于** argv,并且每次迭代循环都会给你这个迭代的相关字符串的第一个字符




printf(" Pointer:%p \tValue:%s \ tFirst:%c \ n",

(void *)argv,

* argv,

** argv);

++ argv;


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

That''s equivalent to **argv, and each iteration through the loop would give
you the first character of the relevant string for that iteration:

printf("Pointer: %p\tValue: %s\tFirst: %c\n",
(void *)argv,
*argv,
**argv);
++argv;

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


ke *** *********@googlemail.com 写道:

大家好,


原谅我确信会有一些简单的问题,但是我有一些问题需要理解:char ** argv看起来像。对于

实例,我通过反复试验知道如果我这样做:


#include< stdio.h>

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

{

int l;

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

{

printf(" Pointer:%p \tValue:%s \ n",argv ++,* argv); < br $>
}


返回0;

}


我调用上面的:


./foo一二三


然后我会看到:


0x10202 foo

0x10204一个

0x10208两个
ox1020c三个


已列出,但我原本预料到需要写一下:


*(*(argv))


所以要取消引用* argv所指向的指针。或者有

我错过了这一点?
Hello all,

Forgive the somewhat simple question I am sure this will be, but I am
having some problem understanding what: char **argv looks like. For
instance, i know through trial and error that if I do this:

#include <stdio.h>

int main(int argc, char *argv[])
{
int l;
for( l=0; l<argc; l++ )
{
printf("Pointer: %p\tValue: %s\n", argv++, *argv);
}

return 0;
}

And i invoke the above as:

./foo one two three

Then I will see:
0x10202 foo
0x10204 one
0x10208 two
ox1020c three

Listed, but I would have expected to have needed to write:

*(*(argv))

So as to dereference what the pointer that *argv pointed to. Or have
I missed the point?



一次剥洋葱一层。


`argv''指向char的指针,我将

写为指向[指向char的指针]的指针强调。


在指针上应用*可以访问指针

指向的东西。所以`* argv''是一个[指向char的指针],这是argv本身指向的东西




现在我们再次申请*,这次是[指向

char]的指针我们从第一步开始。 `** argv''(即

同样是一个'*(* argv))'')因此是一个char,

的第一个字符之一参数字符串。


你可以做的另一件事就是获得理解

指针的多个级别是绘制图片:

char ** char *'s char'的

argv --- argv [0] --- {''f'',''o '',''o'',''\ 0''}

argv [1] --- {''o'',''n'',''e'' ,''\ 0''}

argv [2] --- {''t'',''w'',''o'',''\''' }

argv [3] --- {''t'',''h'',''r'',''e'',''e'',''\\ \\ 0''}

argv [4] == NULL


-

Eric Sosman
es ***** @ ieee-dot-org.inva lid

Peel the onion one layer at a time.

`argv'' is a pointer to a pointer to a char, which I''ll
write as "pointer to [pointer to char]" for emphasis.

Applying `*'' to a pointer accesses the thing the pointer
points to. So `*argv'' is a "[pointer to char]", the thing
that `argv'' itself points at.

Now we apply `*'' again, this time to the "[pointer to
char]" we got from the first step. `**argv'' (which is the
same a `*(*argv))'') is therefore a char, the first char of
one of the argument strings.

Another thing you can do to gain facility in understanding
multiple levels of pointers is to draw a picture:

char** char*''s char''s

argv ---argv[0] ---{ ''f'', ''o'', ''o'', ''\0'' }
argv[1] ---{ ''o'', ''n'', ''e'', ''\0'' }
argv[2] ---{ ''t'', ''w'', ''o'', ''\0'' }
argv[3] ---{ ''t'', ''h'', ''r'', ''e'', ''e'', ''\0'' }
argv[4] == NULL

--
Eric Sosman
es*****@ieee-dot-org.invalid


ke ************ @ googlemail.com 写道:

大家好,


原谅这个有点简单的问题我相信这会是的,但是我有一些问题需要了解:char ** argv看起来像是什么。对于

实例,我通过反复试验知道如果我这样做:


#include< stdio.h>

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

{

int l;

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

{

printf(" Pointer:%p \tValue:%s \ n",argv ++,* argv); < br $>
}


返回0;

}


我调用上面的:


./foo一二三


然后我会看到:


0x10202 foo

0x10204一个

0x10208两个
ox1020c三个


已列出,但我原本预料到需要写一下:


*(*(argv))


所以要取消引用* argv所指向的指针。或者有

我错过了这一点?
Hello all,

Forgive the somewhat simple question I am sure this will be, but I am
having some problem understanding what: char **argv looks like. For
instance, i know through trial and error that if I do this:

#include <stdio.h>

int main(int argc, char *argv[])
{
int l;
for( l=0; l<argc; l++ )
{
printf("Pointer: %p\tValue: %s\n", argv++, *argv);
}

return 0;
}

And i invoke the above as:

./foo one two three

Then I will see:
0x10202 foo
0x10204 one
0x10208 two
ox1020c three

Listed, but I would have expected to have needed to write:

*(*(argv))

So as to dereference what the pointer that *argv pointed to. Or have
I missed the point?



也许两点。


1.不需要parens。 ''char ** argv''就是这样,指向

a指向char的指针。


2.在上面的printf语句中你有两个argv ++和* argv。

因为我们无法知道哪个表达式可能被评估

首先是经典的未定义行为。


#include< stdio.h>


int main(int argc,char ** argv)

{

int l;

for(l = 0; l< argc; l ++){

printf(" Pointer:%p \tValue:%s \ n",argv,* argv);

argv ++;

}

返回0;

}


......就是我这样做的方式。

-

Joe Wright

" ;一切都应该尽可能简单,但不能简单。

---阿尔伯特爱因斯坦---

Maybe two points.

1. The parens are not needed. ''char **argv'' is what it is, a pointer to
a pointer to char.

2. In your printf statement above you have both argv++ and *argv.
Because we have no way to know which expression might be evaluated
first, this is classic Undefined Behavior.

#include <stdio.h>

int main(int argc, char **argv)
{
int l;
for (l = 0; l < argc; l++) {
printf("Pointer: %p\tValue: %s\n", argv, *argv);
argv++;
}
return 0;
}

...is the way I would do it.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---


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

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