指向字符的指针 [英] Pointer to Pointer to character

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

问题描述

一个简单的程序...


#include< iostream>


使用命名空间std;


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


cout<< argv<< endl;

cout<< ; ** argv<< endl;

cout<< argv [0] [0]<< endl;

cout<<& argv [ 0]<< endl;

cout<<&(argv [0] [0])<< endl; //为什么不和& argv [0]相同?


}


为什么& argv [0]& &(argv [0] [0])差异?

a simple program...

#include <iostream>

using namespace std;

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

cout<<argv<<endl;
cout<<**argv<<endl;
cout<<argv[0][0]<<endl;
cout<<&argv[0]<<endl;
cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?

}

why &argv[0] & &(argv[0][0]) difference?

推荐答案

Howachen发布:
Howachen posted:

#include< iostream>
#include <iostream>



您需要包含ostream如果你想使用endl。


You need to include "ostream" if you want to use "endl".


using namespace std;


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


cout<< argv<< endl;
using namespace std;

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

cout<<argv<<endl;



我不认为ostream :: operator<<有一个过载需要一个char **。


I don''t think ostream::operator<< has an overload which takes a char**.


cout<< ** argv<< endl;
cout<<**argv<<endl;



这将打印一个字符。


This will print a single char.


cout<< argv [0 ] [0]<< ENDL;
cout<<argv[0][0]<<endl;



就像这样。 (因为它相当于:


*(*(argv + 0)+ 0)


等于:


** argv


As will this. (Because it''s equivalent to:

*(*(argv + 0) + 0)

which is equal to:

**argv


cout<<& argv [0]<< endl;
cout<<&argv[0]<<endl;



这相当于:


& *(argv + 0)


等于:


argv + 0

等于:


argv


This is equal to:

&*(argv+0)

which is equal to:

argv+0

which is equal to:

argv


cout<<&(argv [0] [0])<< endl; //为什么不一样as& argv [0]?
cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?



这相当于:


& *(*(argv + 0 )+ 0)


等于:


*(argv + 0)


等于:


* argv

-


Frederick Gotham


This is equal to:

&*(*(argv+0)+0)

which is equal to:

*(argv+0)

Which is equal to:

*argv

--

Frederick Gotham


howachen写道:
howachen wrote:

why& argv [0]&&(argv [0] [0不同的e?
why &argv[0] & &(argv[0][0]) difference?



因为argv [0]是char *类型而argv [0] [0]是char类型。所以他们的

地址分别是char **和char *。


-

Phlip
http://www.greencheese.us/ZeekLand < - 不是博客!!!

Because argv[0] is type char * and argv[0][0] is type char. So their
addresses are char** and char* respectively.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


howachen写道:
howachen wrote:

> why& argv [0 ]& &(argv [0] [0])有什么区别?
>why &argv[0] & &(argv[0][0]) difference?


因为argv [0]是char *类型而argv [0] [0]是char类型。所以他们的

地址分别是char **和char *。
Because argv[0] is type char * and argv[0][0] is type char. So their
addresses are char** and char* respectively.



另外因为argv [0]是指针,所以指针本身的地址在这里是

,而argv [0] [ 0]是一个字符,所以它的地址就在那里。


断言(argv [0] ==& argv [0] [0]);


-

Phlip
http: //www.greencheese.us/ZeekLand < - 不是博客!!!

Also because argv[0] is a pointer, so the address of the pointer itself is
here, while argv[0][0] is a character, so its address is there.

assert (argv[0] == &argv[0][0]);

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


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

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