65,209
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int p1 = 2014;
cout << &p1 << endl;
cout << (int*)p1 << endl << endl;
char p2[] = "job";
cout << &p2 << endl;
cout << (int*)p2 << endl;
cin.get();
return 0;
}
理解和讨论之前请先学会如何观察!
学习了(int*)p将指针p的类型转换为整型指针.
首先
cout << (int*)p1 << endl << endl;表示的是p1自身的值,2014转换为十六进制,就是7DE;
P2本身是个char型数组,你(char*)p2
*p2指向数组中第一个元素,(char*)p2 指向数组所有元素,那么打印的肯定也是数组中的值,job;
而你写的(int*)p2
你要知道数组名p2本身就是一个指针了,(int*)p2这样过后,就是把char型的指针强制转换为int型,还是指针,所以输出为0074F7E8