C++ 引用&&C语言二级指针(地址法理解)

江一帆 2020-03-23 AM 5743℃ 7条 3957字 Site load time is:214 ms 百度:已收录

普通变量

#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

引用传递.jpg

指针变量

#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

指针引用传递1.png
指针引用传递2.jpg

指针型变量的引用相当于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

二级指针.png

参考:https://blog.csdn.net/qq_34243930/article/details/81638852
https://www.cnblogs.com/codingmengmeng/p/5865510.html

标签: none

本文最后更新于:2020-03-29 18:47,可能因经年累月而与现状有所差异。

非特殊说明,本博所有文章均为博主原创。

评论啦~



已有 7 条评论


  1. 陈匍蔟
    陈匍蔟

    陈匍蔟:文章真不错https://kan.xiaoxinbk.com/30603.html

    回复 2024-09-20 07:01
  2. ptrzaoipps
    ptrzaoipps

    看的我热血沸腾啊

    回复 2024-09-23 08:51
  3. neggkkjizz
    neggkkjizz

    叼茂SEO.bfbikes.com

    回复 2024-09-23 16:35
  4. bfiimmontl
    bfiimmontl

    怎么收藏这篇文章?

    回复 2024-09-27 12:13
  5. tagcsoebvs
    tagcsoebvs

    不错不错,我喜欢看 https://www.237fa.com/

    回复 2024-10-01 20:34
  6. cofbrmeslq
    cofbrmeslq

    不错不错,我喜欢看 https://www.ea55.com/

    回复 2024-10-04 21:00
  7. optpybjygn
    optpybjygn

    不错不错,我喜欢看 www.jiwenlaw.com

    回复 2024-10-05 19:48
召唤看板娘