函数传值和传址问题

一:传值问题

1:函数形参是非指针变量 传的是值而非地址
示例:
2:函数形参是指针变量 传的是地址而非值
示例:

  1. #include <stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. void swap(int *a,int *b)
  5. {
  6. int *c;
  7. c = a;
  8. a = b;
  9. b = c;
  10. cout<<a<<" "<<b<<endl;
  11. }
  12. int main()
  13. {
  14. int x,y;
  15. x= 1;
  16. y = 2;
  17. swap(&x,&y);
  18. cout<<&x<<" "<<&y<<endl;
  19. printf("x = %d, y = %d\n",x,y);
  20. return 0;
  21. }

变量传值传址问题

示例:

  1. #include <stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int a,b,*c=&a;
  7. int *&p=c;
  8. p=&b;
  9. /*解释:因为p的定义是 *&p 自我理解没有问别人 这种定义的话的引用操作
  10. *p=2;
  11. cout<<p<<endl;
  12. cout<<c<<endl;
  13. cout<<*p<<endl;
  14. cout<<*c<<endl;
  15. }

答案:
0x71feb4
0x71feb4
2
2