1
void swap(int &rx,int &ry)
{
int temp;
temp=rx;
rx=ry;
ry=temp;
}
调用形式:int x=5;int y=6;swap(x,y);
2
void swap(char *&x,char *&y)
{
char *temop
temp=x;
x=y;
y=temp;
}
调用形式:
char *ap="hello";
char *bp="how are you?";
swap(ap,bp);
3(相关:数组)
#include <isotream.h>
void Sum(int array[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=*array;
array++;//OK:array是一个指针,可以作为左值
}
cout<<sum<<endl;
}
void main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
Sum(a,10);
}