交换指针所指的值与交换指针
#include <iostream>
using namespace std;
void SwapVal(int *a, int* b);
void SwapAddr(int* &a, int* &b);
int main()
{
int c = 1, d = 10;
cout << "转换之前" << endl;
cout << "c = " << c << "," << "d = " << d << endl;
SwapVal(&c, &d);
cout << "交换指针所指向的值" << endl;
cout << "c = " << c << "," << "d = " << d << endl;
int m = 1;
int n = 10;
int *a =&m;
int *b = &n;
cout << "\n转换之前" << endl;
cout << "a = " << *a << "," << "b = " << *b << endl;
SwapAddr(a, b);
cout << "交换指针" << endl;
cout << "a = " << *a << "," << "b = " << *b << endl;
system("pause");
return 0;
}
void SwapVal(int *a, int* b)
{//交换指针所指向的值
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void SwapAddr(int* &a, int* &b)
{//交换指针
int *temp;
temp = a;
a = b;
b = temp;
}