gpt4 book ai didi

c - 函数指针和结构

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:26 24 4
gpt4 key购买 nike

我在阅读有关 c 语言结构的文章时,偶然发现了这段代码。我希望有人能帮助我分解这段代码并理解它在做什么。

struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);

who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;

return who;
};

具体来说,这是我不理解的代码部分

*Person_create(char *name, int age, int height, int weight)

最佳答案

* 与类型有关,与功能无关。

您应该将其理解为 Person_create(char *name, int age, int height, int weight) 返回的 struct Person *

所以函数返回一个指向struct Person的指针。

这很常见:

 [return type] func([arguments])

如果你想写一个函数指针,你会:

 [return type] (*func_pointer_name)([arguments])

 struct Person * (*person_create_p)(char *, int, int, int) = &Person_create;

关于c - 函数指针和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205538/

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