用指针函数:
#include <stdio.h>
#include <iostream.h>
void swap(int *p1,int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
void main()
{
int i,j;
cin>>i>>j;
swap(&i,&j);
cout<<i<<","<<j<<endl;
}
利用"引用实参":
#include <stdio.h>
#include <iostream.h>
void swap(int &p1,int &p2)
{
int temp;
temp=p1;
p1=p2;
p2=temp;
}
void main()
{
int i,j;
cin>>i>>j;
swap(i,j);
cout<<i<<","<<j<<endl;
}