您的位置:首页 > 其它

引用为参数实现两个字符串变量的交换

2008-03-31 15:59 363 查看
如题所示,通过调用传递引用的参数,实现两个字符串变量的交换,例如:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: