(C++)为什么会输出0012ff60??指针问题.......

来源:百度知道 编辑:UC知道 时间:2024/06/24 16:44:14
#include "stdafx.h"
#include<iostream>
using namespace std;
void main()
{
int a;
a=1;
int *p=&a;
cout<<p<<endl;
}
为什么输出的不是1??a不是已经赋了1值了吗??

cout<<p<<endl; //输出p 指向的地址
cout<<*p<<endl;//输出p 指向的地址的值

因为定义了int *p,p为32位地址的数据,即用8个16进制数表示为0012ff60,p的值在初始化后就恒定不变了,*p为p地址指向的int型里头的值

p存的是地址,所以你输出的是地址,要输出1应该用cout<<*p<<endl;