zoukankan      html  css  js  c++  java
  • C语言副本机制

    1.除了数组外,其他都有副本机制(包括结构体数组)

    2.结构体作为参数具有副本机制,结构体返回值也有副本机制 。

    3.函数的参数和返回值都有他的副本机制。

    #include<stdio.h>
    int a=10,b=20;
    static int sum(int aa,int bb){
        printf("the aa is 0x%p,%d",&aa,aa);
        printf("
    the bb is 0x%p,%d",&bb,bb);
        aa=100;
        return a+b;
    }
    int main(){
        sum(a,b);
        printf("
    the a is 0x%p,%d",&a,a);
        printf("
    the b is 0x%p,%d",&b,b);
    }

    形参aa和bb是对a和b地址的拷贝。

    #include<stdio.h>
    int array[2]={10,20};
    static int arr(int arra[2]){
        printf("
    the array is 0x%p,%d",arra,arra[0]);
        arra[0]=123;
        return arra[0]+arra[1];
    }
    int main(){
        arr(array);
        printf("
    the array is 0x%p,%d",array,array[0]);
        return 0;
    }

    形参arra传入的是array的实际地址。

    #include<stdio.h>
    typedef struct{
        int ar[10];
        int a;
    }Struct;
    
    Struct my;
    static void fuzhif(Struct mystruct){
        mystruct.a=10;
        mystruct.ar[0]=0;    
        printf("
    the mystruct address is:0x%p,ar address is:0x%p,%d,%d",&mystruct,mystruct.ar,mystruct.a,mystruct.ar[0]);
    }
    
    int main(){
        my.a=100;
        my.ar[0]=100;
        printf("
    the my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);    
        fuzhif(my);
        printf("
    the my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);    
        return 0;
    }

    形参mystruct传入的是结构体my的地址的拷贝。
    #include<stdio.h>
    typedef struct{
        int ar[10];
        int a;
    }Struct;
    
    Struct my;
    
    static void fuzhit(Struct *mystruct){
        mystruct->a=10;
        mystruct->ar[0]=0;    
        printf("
    the mystruct address is 0x%p,ar address is:0x%p,%d,%d",mystruct,mystruct->ar,mystruct->a,mystruct->ar[0]);
    }
    
    int main(){
        my.a=100;
        my.ar[0]=100;
        printf("
    the my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);    
        fuzhit(&my);
        printf("
    the my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);    
        return 0;
    }

    如果形参为结构体变量,那么可以通过结构体指针修改某一结构体变量的值。
  • 相关阅读:
    WBS分析
    需求分析-NABCD
    电梯演讲--2048小游戏
    Git diff 算法
    2020 北京理工大学889 回忆
    抖音后台开发社招面试
    珍宝阁
    2018 北京理工大学复试上机 回忆版
    2018 北京理工大学889 回忆
    做一个博客
  • 原文地址:https://www.cnblogs.com/ligei/p/11363626.html
Copyright © 2011-2022 走看看