简体   繁体   中英

function pointer and struct

I was reading about structures in c, and came across this code. 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) .

So the function returns a pointer to 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM