define a function returning struct pointer

前端 未结 5 1888
天命终不由人
天命终不由人 2021-02-14 06:49

Please bear with me, i m from other language and newbie to c and learning it from http://c.learncodethehardway.org/book/learn-c-the-hard-way.html

struct Person {         


        
5条回答
  •  时光说笑
    2021-02-14 07:01

    It is defined so because it returns a pointer to a struct, not a struct. You assign the return value to a struct Person *, not to struct Person.

    It is possible to return a full struct, like that:

    struct Person Person_create(char *name, int age, int height, int weight)
    {
        struct Person who;
        who.name = strdup(name);
        who.age = age;
        who.height = height;
        who.weight = weight;
        return who;
    }
    

    But it is not used very often.

提交回复
热议问题