函数指针和结构

时间:2016-03-24 16:43:52

标签: c struct function-pointers

我正在阅读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)

1 个答案:

答案 0 :(得分:4)

*与类型有关,而与函数无关。

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

因此该函数返回指向struct Person的指针。

这很常见:

 [return type] func([arguments])

如果你想编写一个函数指针,你可以:

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

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