按值传递:
#include<iostream.h>
void swap(int,int);
void main()
{
int a=10,b=20;
cout<<"a="<<a<<",b="<<b<<endl;
swap(a,b);
cout<<"swapped:"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
引用传递:
类型 &引用名=已有的变量;
#include<iostream.h>
void swap(int&,int&);
void main()
{
int a=10,b=20;
cout<<"a="<<a<<",b="<<b<<endl;
swap(a,b);
cout<<"swapped:"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
地址传递:
#include<iostream.h>
void swap(int*,int*);
void main()
{
int a=10,b=20;
cout<<"a="<<a<<",b="<<b<<endl;
swap(&a,&b);
cout<<"swapped:"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

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



