如题所示,通过调用传递引用的参数,实现两个字符串变量的交换,例如:
char * ap="hello";
char * bp="how are you";
交换的结果使得ap和bp指向的内容分别为:
char * ap="how are you"; ="hello";
char * bp="hello";
好的,下面开始代码:
#include <iostream.h>
void swapstring(char * & ca,char * & cb)
{
char * temp;
temp=ca;
ca=cb;
cb=temp;
}
void main(int argc, char* argv[])
{
char * ap="hello";
char * bp="how are you";
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
swapstring(ap,bp);
cout<<"after swapping..."<<endl;
cout<<"ap: "<<ap<<endl;
cout<<"bp: "<<bp<<endl;
}
本文介绍了一种使用C++通过传递引用的方式实现两个字符串指针变量的交换方法,并提供了完整的示例代码。
573

被折叠的 条评论
为什么被折叠?



