0

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)
1
  • 2
    The * is related to the type, not the function. You should read it as struct Person * and Person_create(char *name, int age, int height, int weight). So the function returns a pointer to struct Person. Commented Mar 24, 2016 at 16:45

1 Answer 1

4

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])

i.e.

 struct Person * (*person_create_p)(char *, int, int, int) = &Person_create;
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I see. So the function portion of the code takes precedence over the struct, which makes the code read, as, function returns this structure. Great, thanks for the quick response. Very clear explanation!
@justthom8 It returns a pointer to a structure in this case, although it is also possible to return an actual structure value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.