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

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

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

#include<stdio.h>
int a=,b=;
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=;
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);
}

C语言副本机制-LMLPHP

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

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

C语言副本机制-LMLPHP

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

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

C语言副本机制-LMLPHP

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

C语言副本机制-LMLPHP

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