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

本文介绍了一种使用C++通过传递引用的方式实现两个字符串指针变量的交换方法,并提供了完整的示例代码。

如题所示,通过调用传递引用的参数,实现两个字符串变量的交换,例如:

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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值