gpt4 book ai didi

C - 将结构地址作为函数参数传递

转载 作者:行者123 更新时间:2023-12-02 09:36:51 25 4
gpt4 key购买 nike

只是想在这里解决一个小概念问题。在教科书的这个示例中,它尝试通过引用将结构体的地址传递给下面的函数。

在我看来,我认为既然我传递了“&address”,我就必须在函数中取消引用它才能使用结构操作,例如这样

(*planet)->name;   

,这让他们在函数中使用“&planet”变得更加糟糕,这不是就像

&(&planet) 

在我看来,他们只是在结构地址上使用结构运算符,而不是结构本身。

if (read_planet_ptr(&planet) != EOF) {

...


int read_planet_ptr(planet_t *planet) {

int nvals_read;
printf("Enter %s:\n", PLANETPROMPT);
nvals_read = scanf("%s %s %lf %lf %lf",
planet->name,
planet->orbits,
&planet->distance,
&planet->mass,
&planet->radius);
if (nvals_read != 5) {
return EOF;
} else {
return 0;
}
}

非常感谢任何帮助

最佳答案

&planet->distance 实际上被计算为 &(planet->distance),因此意味着获取结构体成员的地址,其中 scanf( ) 需要使用输入值更新该成员。

关于C - 将结构地址作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922337/

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