钱能C++习题9.3
编制程序,调用传递引用的参数,实现两个字符串变量的交换.
例如:
ap:"how are you?"
bp:"hello"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>


using namespace std;

void swapstring(char * & r1,char * & r2)
{
char * tmp=r1;
r1=r2;
r2=tmp;
}

int _tmain(int argc, _TCHAR* argv[])
{
char * ap="hello";
char * bp="how are you?";
cout<<ap<<","<<bp<<endl;
swapstring(ap,bp);
cout<<ap<<","<<bp<<endl;

int i;
cin>>i;
return 0;
}
这道题应该考核的只是对指针的引用的学习.
char *是char型的指针,char * & r1则是对char * 指针的引用.
例如:
ap:"how are you?"
bp:"hello"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;
void swapstring(char * & r1,char * & r2)
{
char * tmp=r1;
r1=r2;
r2=tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
char * ap="hello";
char * bp="how are you?";
cout<<ap<<","<<bp<<endl;
swapstring(ap,bp);
cout<<ap<<","<<bp<<endl;
int i;
cin>>i;
return 0;
}这道题应该考核的只是对指针的引用的学习.
char *是char型的指针,char * & r1则是对char * 指针的引用.
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。



浙公网安备 33010602011771号