zoukankan      html  css  js  c++  java
  • C++函数调用之——值传递、指针传递、引用传递

    1、简介

      1、值传递:形参时实参的拷贝,改变函数形参并不影响函数外部的实参,这是最常用的一种传递方式,也是最简单的一种传递方式。只需要传递参数,返回值是return考虑的;使用值传递这种方式,调用函数不对实参进行操作,也就是说,即使形参的值发生改变,实参的值也完全不受影响。

      2、指针传递:指针传递其实是值传递的一种,它传递的是地址。值传递过程中,被调函数的形参作为被调函数的局部变量来处理,即在函数的栈中有开辟了内存空间来存放主调函数放进来实参的值,从而成为一个副本。因为指针传递的是外部参数的地址,当调用函数的形参发生改变时,自然外部实参也发生改变。

      3、引用传递:被调函数的形参虽然也作为局部变量在栈中开辟了内存空间,但在栈中放的是由主调函数放进来的实参变量的地址。被调函数对形参的任何操作都被间接寻址,即通过栈中的存放的地址访问主调函数中的中的实参变量(相当于一个人有两个名字),因此形参在任意改动都直接影响到实参。

    2、例程

       1、值传递
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include <iostream>
    using namespace std;
    void swap(int,int);
    int main()
    {
       int a=1,b=2;
       cout<<"a="<<a<<",b="<<b<<endl;
       swap(a,b);
       cout<<"a="<<a<<",b="<<b<<endl;
       return 0;
    }
     void swap(int x,int y)
    {
       int p=x;
       x=y;
       y=p;
    }

      

      2、指针传递
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #include <iostream>
    using namespace std;
    void swap(int *x,int *y);
    int main()
    {
       int a=1,b=2;
       cout<<"a="<<a<<",b="<<b<<endl;
       cout<<"&a="<<&a<<",&b="<<&b<<endl;
       cout<<"********1**********"<<endl;
       swap(&a,&b);
       cout<<"a="<<a<<",b="<<b<<endl;
       cout<<"&a="<<&a<<",&b="<<&b<<endl;
       cout<<"********3**********"<<endl;
       return 0;
    }
     void swap(int *x,int *y)
    {
       int p=*x;  //int p; p=*x;
       *x=*y;
       *y=p;
       cout<<"x="<<x<<",y="<<y<<endl;
       cout<<"*x="<<*x<<",*y="<<*y<<endl;
       cout<<"&x="<<&x<<",&y="<<&y<<endl;
       cout<<"********2**********"<<endl;
    }

     int *x=&a;//用于指针传递,x有自己独立的内存地址,存储内容是a的地址,*x是存a的值。

    根据图1 可知:x(y)是一个指向外部实参地址的指针,*x(*y)是指针的内容,如果改变了*x(*y)也必然导致外部实参的改变。

    这是因为x或y的指针指向a或b的地址,因此*x*y值的交换导致外部实参发生变化。

      

      3、引用传递
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #include <iostream>
    using namespace std;
    void swap(int &x,int &y);
    int main()
    {
       int a=1,b=2;
       cout<<"a="<<a<<",b="<<b<<endl;
       cout<<"&a="<<&a<<",&b="<<&b<<endl;
       cout <<"*******1**********"<<endl;
       swap(a,b);
       cout<<"a="<<a<<",b="<<b<<endl;
       cout<<"&a="<<&a<<",&b="<<&b<<endl;
       cout <<"*******4**********"<<endl;
       return 0;
    }
     void swap(int &x,int &y)
    {
       cout<<"x="<<x<<",y="<<y<<endl;
       cout<<"&x="<<&x<<",&y="<<&y<<endl;
       cout <<"*******2**********"<<endl;
        int p=x; 
        x=y;
        y=p;
       cout<<"x="<<x<<",y="<<y<<endl;
       cout<<"&x="<<&x<<",&y="<<&y<<endl;
       cout <<"*******3**********"<<endl;
    }

      

    引用实参和形参时一样的,只是名字不同而已。

    原文地址 https://www.cnblogs.com/happying30/p/9484860.html

  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/brady-wang/p/15078744.html
Copyright © 2011-2022 走看看