20191221-指针题

代码1: 

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "string"

  4. using namespace std;

  5. int main()

  6. {

  7. int a=5;

  8. cout<<&a<<endl;

  9. a=6;

  10. cout<<&a<<endl;

  11. int *p=&a;

  12. cout<<p<<endl;

  13. *p=1;

  14. cout<<p<<endl;

  15. cout<<a<<endl;  


  16. 以上是一道指针题,假如第一个COUT的输出结果是:0033EFD0,请写出后4个COUT分别输出的内容:

  17.  

  18.       0033EFD0            0033EFD0              0033EFD0                  1            

  19.  

运行结果:

 

 

解题思路:

    指针是有数据类型的,如上面代码中的INT,*P指向变量a的内存地址启始处,等同于&a,所以改变*p的值就是改变a的值。

 

 

代码2:

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "string"

  4. using namespace std;

  5. void jzj(int &a);

  6. int main()

  7. {

  8.  int jzj1=10;

  9.  jzj(jzj1);

  10.  cout<<jzj1<<endl;  

  11. }

  12. void jzj(int &a)

  13. {

  14.  int *b=&a;

  15.  *b=17;

  16. }

  17.  

输出:          17           

 

解题思路:

    由于传进jzj方法的参数是jzj1的地址,然后在方法中定义一个int类型的指针*b指向参数的地址,修改 *b的值,变量a(jzj1)的值也会相应修改,所以输出17 。

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