20191222-指针题2(指针+地址+引用变量)

代码1: 

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "string"

  4. using namespace std;

  5. int main()

  6.    int rats=101;

  7.    int *pt=&rats;  //101

  8.    int & rodents=*pt; //101

  9.    int butt=50;

  10.    pt=&butt;

  11.    cout<<pt<<endl;

  12. }

 

 

代码2:

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "string"

  4. using namespace std; 
    void swpr(int &a,int &b); 

  5. int main()

  6. { int a=3,b=5; 
    swpr(a,b);

  7. cout<<"a="<<a<<endl;

  8. cout<<"b="<<b;}

  9. void swpr(int & a,int & b)

  10. {

  11.   int temp;

  12.   temp=a;

  13.   a=b;

  14.   b=temp;}

运行结果:

 

思路分析,注意swpr方法传递的是引用参数,所以a,b的值对调了,如果去掉&,则传递的是临时参数,不改变a,b的值。

posted @ 2020-05-21 08:12  财盛  阅读(126)  评论(0编辑  收藏  举报