函数传值和传址问题
一:传值问题
1:函数形参是非指针变量 传的是值而非地址
示例:
2:函数形参是指针变量 传的是地址而非值
示例:
#include <stdio.h>#include<iostream>using namespace std;void swap(int *a,int *b){int *c;c = a;a = b;b = c;cout<<a<<" "<<b<<endl;}int main(){int x,y;x= 1;y = 2;swap(&x,&y);cout<<&x<<" "<<&y<<endl;printf("x = %d, y = %d\n",x,y);return 0;}
变量传值传址问题
示例:
#include <stdio.h>#include<iostream>using namespace std;int main(){int a,b,*c=&a;int *&p=c;p=&b;/*解释:因为p的定义是 *&p 自我理解没有问别人 这种定义的话的引用操作*p=2;cout<<p<<endl;cout<<c<<endl;cout<<*p<<endl;cout<<*c<<endl;}
答案:
0x71feb4
0x71feb4
2
2
