gpt4 book ai didi

c - 空指针作为参数

转载 作者:太空狗 更新时间:2023-10-29 17:07:19 24 4
gpt4 key购买 nike

<分区>

以下 C 代码段:

[...] 
void f1(void* a){
printf("f(a) address = %p \n",a);
a = (void*)(int*)malloc(sizeof(int));

printf("a address = %p \n",a);
*(int*)a = 3;

printf("data = %d\n",*(int*)a);
}

void f(void){
void* a1=NULL;
printf("a1 address = %p \n",a1);

f1(a1);

printf("a1 address = %p \n",a1);
printf("Data.a1 = %d\n",*(int*)a1);
}
[...]

结果

a1 address = (nil) 
f(a) address = (nil)
a address = 0xb3f010
data = 3
a1 address = (nil)
Segmentation fault (core dumped)

为什么a1 不在函数中保留分配给它的地址?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com