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;

    扫描二维码关注公众号,回复: 11230544 查看本文章
  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的值。

猜你喜欢

转载自www.cnblogs.com/whcsrj/p/12928160.html