|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.传递传向指针的引用:
#include<iostream>
using namespace std;
//void reset(int *ip)
//{
// *ip = 0;
//}
//
//int compare(int i ,const int *ip)
//{
// if(i > *ip)
// return i;
// else
// return *ip;
//}
void ptrswap(int *&v1, int *&v2)
{
int *tmp = v2;
v2 = v1;
v1 = tmp;
}
int main()
{
//int i = 1;
//cout << i << endl;
//reset(&i);
//cout << i << endl;
//int a = 3 , b = 5;
//int c = compare(a, &b);
//cout << a << ' '<< b << ' ' << c << endl;
int i = 10, j = 20;
int *pi = &i;
int *pj = &j;
cout << "before prtswap():" << endl;
cout << " *pi " << *pi << " pi地址: " << pi << endl;
cout << " *pj " << *pj << " pj地址: " << pj << endl;
ptrswap(pi,pj);
cout << "after prtswap():" << endl;
cout << " *pi " << *pi << " pi地址: " << pi << endl;
cout << " *pj " << *pj << " pj地址: " << pj << endl;
system("pause");
return 0;
}
before prtswap():
*pi 10 pi地址: 001BFB08
*pj 20 pj地址: 001BFAFC
after prtswap():
*pi 20 pi地址: 001BFAFC
*pj 10 pj地址: 001BFB08
请按任意键继续. . .
传递指针的引用,即传递的两个指针互相交换指向对象,不改变指向对象的值。pi 和pj互相交换了指向对象,pi 指向了j,pj指向了i,没有改变i,和j的值 |
|