void swap1(int *, int *);
void swap2(int &,int &);
int main(){
int x = 1, y = 2;
int *p = &x;
int *q = &y;
swap1(p,q); //若原型是*p,*q則寫p,q即可。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap2(x,y); //若原型是&x,&y則寫x,y即可。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap1(&x,q);//若原型是*p,*q也可以寫&x,&y。
cout<<" x is "<<x<<" and y is "<<y<<endl;
swap2(*p,y);//若原型是&x,&y則也可以寫*p,*q。
cout<<" x is "<<x<<" and y is "<<y<<endl;
return 0;
}
void swap1(int *p, int *q){
int temp = 0;
temp = *p;
*p = *q;
*q = temp;
}
void swap2(int &i, int&j){
int temp = 0;
temp = i;
i = j;
j = temp;
}作者: return 時間: 2019-3-10 10:48
#include <iostream>
#include <string>
using namespace std;
void change1(int&); // 給值時不用另外加指標符號
void change2(int*); // 給值時要加上&的指標符號
int main(){
int x = 100;
int y = 100;
change1(x);
cout<<"x is now "<<x<<endl;
change2(&y);
cout<<"y is now "<<y<<endl;