c++基础知识-指针
1.指针
通俗来说,指针就是地址,其中*为指针符号,&为取地址符号
1.1 指针
#include <iostream>
using namespace std;
int main()
{
int a = 10;//定义int变量
int * p;//定义int指针,注意*
p = &a;//指针p指向a的地址,注意&
cout<<p<<endl;//00F6F958 //直接输出是地址
cout<<*p<<endl;//10 //输出对应变量
return 0;
}
1.2 空指针
#include <iostream>
using namespace std;
int main()
{
int * p = NULL;
cout<<*p<<endl;//空指针指向的内存不能访问,只用于初始化
return 0;
}
1.3 野指针
#include <iostream>
using namespace std;
int main()
{
int * p = (int *)0x1100;//指向一个未知的地址,野指针
cout<<*p<<endl;//无法访问
return 0;
}