#include <iostream>
using namespace std;
void swap(const char *&p1,const char *&p2) //地址的交换
{
const char *temp; //定义一个中间变量
if (p1 == p2) //两个指针指向同一个地址不需要交换
{
return;
}
else
{
temp = p1;
p1 = p2;
p2 = temp;
}
}
void out(char *p) //输出字符串
{
for (int i=0;p[i]!='\0';++i)
{
cout << p[i];
}
}
int main()
{
char *p1 = "hello";
char *p2 = "good";
cout << "未交换前:" << endl;
cout << "p1: ";
out(p1);
cout << endl;
cout << "p2: ";
out(p2);
cout << endl;
cout << "交换后:" << endl;
swap(p1,p2);
cout << "p1: ";
out(p1);
cout << endl;
cout << "p2: ";
out(p2);
cout << endl;
return 0;
}