c语言怎么建立副本文件,C语言副本机制

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

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

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

#include

int a=10,b=20;

static int sum(int aa,int bb){

printf("the aa is 0x%p,%d",&aa,aa);

printf("\nthe bb is 0x%p,%d",&bb,bb);

aa=100;

return a+b;

}

int main(){

sum(a,b);

printf("\nthe a is 0x%p,%d",&a,a);

printf("\nthe b is 0x%p,%d",&b,b);

}

48257fc11523b655dda7dd50da193e40.png

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

#include

int array[2]={10,20};

static int arr(int arra[2]){

printf("\nthe array is 0x%p,%d",arra,arra[0]);

arra[0]=123;

return arra[0]+arra[1];

}

int main(){

arr(array);

printf("\nthe array is 0x%p,%d",array,array[0]);

return 0;

}

ca358ad58d321333cf06994e8a5f557d.png

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

#include

typedef struct{

int ar[10];

int a;

}Struct;

Struct my;

static void fuzhif(Struct mystruct){

mystruct.a=10;

mystruct.ar[0]=0;

printf("\nthe 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("\nthe my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);

fuzhif(my);

printf("\nthe my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);

return 0;

}

d1b261424cc994f491053660c76a3533.png

形参mystruct传入的是结构体my的地址的拷贝。

#include

typedef struct{

int ar[10];

int a;

}Struct;

Struct my;

static void fuzhit(Struct *mystruct){

mystruct->a=10;

mystruct->ar[0]=0;

printf("\nthe 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("\nthe my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);

fuzhit(&my);

printf("\nthe my address is:0x%p,ar address is:0x%p,%d,%d",&my,my.ar,my.a,my.ar[0]);

return 0;

}

707fc813574e331641283ff6a78d82dd.png

如果形参为结构体变量,那么可以通过结构体指针修改某一结构体变量的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值