C++中指针的传递性
指针值的传递性
下面展示一些 实例代码
---->
#include<iostream>
using namespace std;
void show(const int * p1,const int *p2)
{
cout<<"p1-->指向的地址是"<<p1<<" "<<"值为"<<*p1<<endl;
cout<<"p2-->指向的地址是"<<p2<<" "<<"值为"<<*p2<<endl;
}
int main()
{
int test = 100;
int *p1 = &test;
int *p2 = p1;
show(p1,p2);
*p1 = 0; //只是改变p1的值
show(p1,p2);
}
**********************************************************************************************************************************************************
运行结果为p1,p2的值都发生改变因为p1,p2操作的是同一块内存 内存中数据变了指针不知道 他只是指向那一块数据
指针指向的传递性
下面展示一些 实例代码
---->
#include<iostream>
using namespace std;
void show(const int * p1,const int *p2)
{
cout<<"p1-->指向的地址是"<<p1<<" "<<"值为"<<*p1<<endl;
cout<<"p2-->指向的地址是"<<p2<<" "<<"值为"<<*p2<<endl;
cout<<endl;
}
int main()
{
int test = 100;
int *p1 = &test;
int *p2 = p1;
show(p1,p2);
int change = 555;
p1 = &change; //这里是改变他的指向的地址
show(p1,p2);
}