[英]function pointer and struct
I was reading about structures in c, and came across this code. 我正在阅读有关c中的结构的内容,并遇到了这段代码。 I was hoping somebody could help me break this code down and understand what its' doing. 我希望有人可以帮助我分解这段代码,并了解它在做什么。
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;
};
Specifically, this is the portion of the code that I don't understand 具体来说,这是我不理解的部分代码
*Person_create(char *name, int age, int height, int weight)
The *
is related to the type, not the function. *
与类型有关,与功能无关。
You should read it as struct Person *
returned by Person_create(char *name, int age, int height, int weight)
. 您应该将其读取为Person_create(char *name, int age, int height, int weight)
返回的struct Person *
Person_create(char *name, int age, int height, int weight)
。
So the function returns a pointer to struct Person
. 因此该函数返回一个指向struct Person
的指针。
it's a common: 这是常见的:
[return type] func([arguments])
If you wanted to write a function pointer, you would have: 如果要编写函数指针,则将具有:
[return type] (*func_pointer_name)([arguments])
ie 即
struct Person * (*person_create_p)(char *, int, int, int) = &Person_create;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.