1.指针变量允许将一个整数经强制转换后赋值给指针变量
Eg:
float *fp;
fp = (float *)5000;//意义:将5000作为一个地址赋给指针变量fp
2.指针变量指向初始化:
①int m,*p;
p = &m;
②int m,*p=&m;
3.交换两指针的指向:
#include<iostream>
using namespace std;
void main(){
int x = 10,y = 20;
int *p1 = &x,*p2 = &y,*t;
cout<<*p1<<' '<<*p2<<endl; //交换前
t = p1;p1 = p2;p2 = t; //交换
cout<<*p1<<' '<<*p2<<endl;
}
------------------
10 20
20 10
3.引用变量,指针变量引用场景:
①func(int &c,int &h){
...
}
void main(){
...
func(x,n);
}
-------------
解析: &c = x; &h = n;//c与h均为引用变量
②void func(int *p2){
...
}
void main(){
int x=10,*p1;
p1 = &x;
func(p); //等同于: *p2 = &x;
}