读书人

C++指针难解啊坐待牛人指点

发布时间: 2012-11-03 10:57:43 作者: rapoo

C++指针难解啊,坐等牛人指点
#include <iostream>
using namespace std;
int main()
{
char **p;
char* name[5]= {"hello", "good","world", "bye"};

p = name;
cout << name << endl;
cout << &name[0] << endl;
cout << name[0] << endl;
cout << &name[1] << endl;
cout << name[1] << endl << endl;

cout << p << endl;
cout << *p << endl;
//cout << **P << endl;
return 0;
}

结果输出如下:
0x22ff28
0x22ff28
hello
0x22ff2c
good

0x22ff28
hello

不明白这句:cout << name[0] << endl; 为什么输出hello?name[0]里存的不应该是指向字符串的地址吗?还有就是这句cout << *p << endl;也不是很理解,能不能详细介绍下指针,或者有个技术连接也行。谢谢了


[解决办法]
很难理解?
输出char*会从该地址一直输出,遇到0即字符串结束符为止。
[解决办法]
指针真的是很难的,而且和数组不一样的,lz区分开理解
[解决办法]
char* name[5]= {"hello", "good","world", "bye"};
name[0]里存的就是hello
字符串指针数组
[解决办法]
lz从不结贴。
你的结果好理解,来一个不好理解的结果:http://codepad.org/TfDFSrry
[解决办法]
char* name[5]这是一个二维数组,等价于char name[5][],个人认为name[0]可以理解为这个二维数组的第一行
[解决办法]
1, 数组到指针的转换
2, 函数重载

C/C++ code
#include <iostream>using namespace std;int main(){  //int a[5];  //int* p = a; //转换为 int*,属于标准转换之一  char **p;  char* name[5]= {"hello", "good","world", "bye"};  p = name; //同理name的类型转为char* *  cout << name << endl;  // std::ostream::operator<<重载了void*,打出地址  cout << &name[0] << endl; //name[0]的类型: char*,所以 &name[0]的类型为char**,结果同上  cout << name[0] << endl; //name[0]的类型: char*,operator>>(std::ostream& char*),结果打印字符串  cout << &name[1] << endl;  cout << name[1] << endl << endl;  cout << p << endl; //p的类型char**, 所以调用std::osteram::operator<<(void const*)  cout << *p << endl; //*p的类型char*, 所以调用std::operator<<(std::iostream&, char const*), 打印字符串  //cout << **P << endl;//**p的类型char, 所以调用std::operator<<(std::iostream&, char),打印字符的值  return 0;} 

读书人网 >C++

热点推荐