年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8332|回复: 1

函数中值传递与地址传递的区别

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-3 22:07:33 | 显示全部楼层 |阅读模式
    /*
    1、本程序示例旨在说明传递值与传递地址对值的影响;
    2、函数passValues()修改的是基本数据类型的值,因此传递进去的是变量的值;
    3、函数passValuesAddress()修改的是数组类型的值,传递的是数组的地址;
    4、传递值,对原值没有影响,两变量的地址不同;
    5、传递地址,修改的原值,因此原值会因此而被修改。
    */
    #include <stdio.h>

    void passValues(int n) {

        // 修改的内容仅在本作用域范围内生效;
        n = 100;

        // 打印临时变量n的地址;
        printf("Address of n is: %p\n", &n);

    }

    void passValuesAddress(char array[]) {

        // 修改值对原值产生影响
        array[0] = 'a';

       // 该地址即传递进来的数组变量的地址;
        printf("Address of array[0] is: %p\n", &array[0]);

    }


    int main() {

        int a = 10;

        // 打印临时变量a的地址;
        printf("Address of a is: %p\n", &a);

        printf("a = %d\n", a);

        // 值传递函数;
        passValues(a);

        // 在本作用域内,a的值没有被修改过,因而保持不变;
        printf("a = %d\n", a);

        char chars[5] = {'A', 'B', 'C', 'D', 'E'};

        // 打印数组元素的地址;
        printf("Address of chars[0] is: %p\n", &chars[0]);

        // 打印数组元素的值;
        printf("chars[0] = %c\n", chars[0]);

        // 地址传递函数;
        passValuesAddress(chars);

        // 元素的值会受函数的影响;
        printf("chars[0] = %c\n", chars[0]);

        return 0;

    }

    /************************************************************
    输出结果为:
    Address of a is: 0x7fff50397c68
    a = 10
    Address of n is: 0x7fff50397c2c
    a = 10
    Address of chars[0] is: 0x7fff50397c63
    chars[0] = A
    Address of array[0] is: 0x7fff50397c63

    chars[0] = a
    */

    常规整形变量a的值没有因此而改变,但数组元素的值因此而发生了改变;

  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

     楼主| 发表于 2014-3-3 22:08:18 | 显示全部楼层
    附上可copy代码:
    1. /*
    2. 1、本程序示例旨在说明传递值与传递地址对值的影响;
    3. 2、函数passValues()修改的是基本数据类型的值,因此传递进去的是变量的值;
    4. 3、函数passValuesAddress()修改的是数组类型的值,传递的是数组的地址;
    5. 4、传递值,对原值没有影响,两变量的地址不同;
    6. 5、传递地址,修改的原值,因此原值会因此而被修改。
    7. */
    8. #include <stdio.h>
    9. void passValues(int n) {
    10.     // 修改的内容仅在本作用域范围内生效;
    11.     n = 100;
    12.    
    13.     // 打印临时变量n的地址;
    14.     printf("Address of n is: %p\n", &n);
    15.    
    16. }
    17. void passValuesAddress(char array[]) {
    18.     // 修改值对原值产生影响
    19.     array[0] = 'a';
    20.    
    21.     // 该地址即传递进来的数组变量的地址;
    22.     printf("Address of array[0] is: %p\n", &array[0]);
    23. }
    24. int main() {
    25.     int a = 10;
    26.    
    27.     // 打印临时变量a的地址;
    28.     printf("Address of a is: %p\n", &a);
    29.    
    30.     printf("a = %d\n", a);
    31.    
    32.     // 值传递函数;
    33.     passValues(a);
    34.    
    35.     // 在本作用域内,a的值没有被修改过,因而保持不变;
    36.     printf("a = %d\n", a);
    37.    
    38.     char chars[5] = {'A', 'B', 'C', 'D', 'E'};
    39.    
    40.     // 打印数组元素的地址;
    41.     printf("Address of chars[0] is: %p\n", &chars[0]);
    42.    
    43.     // 打印数组元素的值;
    44.     printf("chars[0] = %c\n", chars[0]);
    45.    
    46.     // 地址传递函数;
    47.     passValuesAddress(chars);
    48.    
    49.     // 元素的值会受函数的影响;
    50.     printf("chars[0] = %c\n", chars[0]);
    51.    
    52.     return 0;
    53.    
    54. }
    复制代码
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-4 03:54 , Processed in 0.049683 second(s), 23 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表