运算符:
1:& 取地址运算符,&a运算结果是一个指针,指针的类型是a的类型前加*,指针所指向的类型是a的类型, &a所指向的地址是a的地址。
2:* 间接运算符,p为指针,*p类型是p所指向的类型,占用的地址也是p所指向的地址
例如:
int a=10; int * b=&a;
b=10; b为a的地址,b占用的地址是a的地址。
指针和结构体的关系
1:-> :指向运算符
2:. : 成员运算符
对于一个结构体:
struct node{
int a,b,c;
};
struct node ans={1,2,3};
struct node *ptr =&ans;
//访问 ans中的a
cout<<ans.a<<endl;//方法一
cout<<ptr->a<<endl;//方法二
cout<<(*ptr).a<<endl;//方法三
//int类型指针访问 a,b
int * iptr= (int *)&ans;
cout<<*iptr<<endl;
cout<<*(iptr+1)<<endl;
null和nullptr
null : 实质为宏定义,相当于0
C中:
#define NULL ((void *)0)
C++中:
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
nullptr :c++11后为了解决NULL在部分情况下可能被编译器识别为整型的情况下引入, 代表空指针,在C++11支持的地方尽量选择nullptr