代码:
#include <iostream>
using namespace std;
//交换两个字符串,通过引用
void Swap(char*& str1, char*& str2)
{
char* temp = str1;
str1 = str2;
str2 = temp;
}
//交换两个字符串,通过二级指针
void Swap2(char** str1, char** str2)
{
char* temp = *str1;
*str1 = *str2;
*str2 = temp;
}
int main()
{
char* str1 = "dododo";
char* str2 = "gogogo";
cout << "交换前:" << endl;
cout << "str1= " << str1 << " ";
cout<<"str2= " << str2 << endl;
Swap(str1, str2);
cout << "交换后:" << endl;
cout << "str1= " << str1 << " ";
cout << "str2= " << str2 << endl;
Swap2(&str1, &str2);
cout << "再次交换后:" << endl;
cout << "str1= " << str1 << " ";
cout << "str2= " << str2 << endl;
cout << endl;
system("pause");
return 0;
}
测试: