普通变量
#include <iostream>
using namespace std;
void swap(int &x, int &y) { //引用传递,可以理解为a就是x,x就是a,只不过名字不一样
cout << "函数内-start" << endl;
cout << "&x:" << &x << " &y:" << &y << endl;
cout << "x:" << x << " y:" << y << endl;
cout << "开始交换" << endl;
int tmp;
tmp = x;
x = y;
y = tmp;
cout << "&x:" << &x << " &y:" << &y << endl;
cout << "x:" << x << " y:" << y << endl;
cout << "函数内-end" << endl;
}
int main() {
int a = 2, b = 3;
cout << "before a:" << a << " b:" << b << endl;
cout << "before &a:" << &a << " &b:" << &b << endl;
swap(a, b);
cout << "later a:" << a << " b:" << b << endl;
cout << "later &a:" << &a << " &b:" << &b << endl;
return 0;
}
结果
before a:2 b:3
before &a:0x61fe1c &b:0x61fe18
函数内-start
&x:0x61fe1c &y:0x61fe18
x:2 y:3
开始交换
&x:0x61fe1c &y:0x61fe18
x:3 y:2
函数内-end
later a:3 b:2
later &a:0x61fe1c &b:0x61fe18
指针变量
#include <iostream>
using namespace std;
void swap(int *&x, int *&y) {
cout << "函数内-start" << endl;
cout << "&x:" << &x << " &y:" << &y << endl;
cout << "x:" << x << " y:" << y << endl;
cout << "开始交换" << endl;
int *tmp;//x,y类型是int*
tmp = x;
x = y;
y = tmp;
cout << "&x:" << &x << " &y:" << &y << endl;
cout << "x:" << x << " y:" << y << endl;
cout << "函数内-end" << endl;
cout<<endl;
}
int main() {
int a = 2, b = 3;
int *p=&a,*q=&b;
cout << "before a:" << a << " b:" << b << endl;
cout << "before &a:" << &a << " &b:" << &b << endl;
cout << "before *p:" << *p << " &p:" << &p<<" p:"<<p<< endl;
cout << "before *q:" << *q << " &q:" << &q<<" q:"<<q << endl;
cout<<endl;
swap(p, q);
cout << "later a:" << a << " b:" << b << endl;
cout << "later &a:" << &a << " &b:" << &b << endl;
cout << "before *p:" << *p << " &p:" << &p<<" p:"<<p<< endl;
cout << "before *q:" << *q << " &q:" << &q<<" q:"<<q << endl;
return 0;
}
结果:
before a:2 b:3
before &a:0x61fe1c &b:0x61fe18
before *p:2 &p:0x61fe10 p:0x61fe1c
before *q:3 &q:0x61fe08 q:0x61fe18
函数内-start
&x:0x61fe10 &y:0x61fe08
x:0x61fe1c y:0x61fe18
开始交换
&x:0x61fe10 &y:0x61fe08
x:0x61fe18 y:0x61fe1c
函数内-end
later a:2 b:3
later &a:0x61fe1c &b:0x61fe18
before *p:3 &p:0x61fe10 p:0x61fe18
before *q:2 &q:0x61fe08 q:0x61fe1c
指针型变量的引用相当于C语言中的二级指针
#include <cstdio>
void f1(int **b) {
printf("函数-start\n");
printf("**b=%d,*b=%#X,*&b=%#X,&*b=%#X\n", **b,*b,*&b,&*b);
printf("&b=%#X,b=%#X\n",&b,b);
int p=2;
printf("p=%d,&p=%#X\n", p,&p);
int *q=&p;
printf("*q=%d,q=%#X,&q=%#X\n", *q,q,&q);
*b=q;
printf("*b=q;\n");
printf("**b=%d,*b=%#X,*&b=%#X,&*b=%#X\n", **b,*b,*&b,&*b);
printf("&b=%#X,b=%#X\n",&b,b);
printf("函数-end\n");
}
int main() {
int a=1;
int *s=&a;
printf("a=%d,&a=%#X\n", a,&a);
printf("*s=%d,&s=%#X,s=%#X\n", *s,&s,s);
f1(&s);
printf("*s=%d,&s=%#X,s=%#X\n", *s,&s,s);
printf("a=%d,&a=%#X\n", a,&a);
return 0;
}
结果:
a=1,&a=0X61FE1C
*s=1,&s=0X61FE10,s=0X61FE1C
函数-start
**b=1,*b=0X61FE1C,*&b=0X61FE10,&*b=0X61FE10
&b=0X61FDF0,b=0X61FE10
p=2,&p=0X61FDDC
*q=2,q=0X61FDDC,&q=0X61FDD0
*b=q;
**b=2,*b=0X61FDD4,*&b=0X61FE10,&*b=0X61FE10
&b=0X61FDF0,b=0X61FE10
函数-end
*s=2,&s=0X61FE10,s=0X61FDD4
a=1,&a=0X61FE1C
参考:https://blog.csdn.net/qq_34243930/article/details/81638852
https://www.cnblogs.com/codingmengmeng/p/5865510.html
陈匍蔟:文章真不错https://kan.xiaoxinbk.com/30603.html
看的我热血沸腾啊
叼茂SEO.bfbikes.com
怎么收藏这篇文章?
不错不错,我喜欢看 https://www.237fa.com/
不错不错,我喜欢看 https://www.ea55.com/
不错不错,我喜欢看 www.jiwenlaw.com
哈哈哈,写的太好了https://www.cscnn.com/