define a function returning struct pointer

前端 未结 5 1908
天命终不由人
天命终不由人 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:04

    The function returns who, which is a struct Person * - a pointer to a structure. The memory to hold the structure is allocated by malloc(), and the function returns a pointer to that memory.

    If the function were declared to return struct Person and not a pointer, then who could also be declared as a structure. Upon return, the structure would be copied and returned to the caller. Note that the copy is less efficient than simply returning a pointer to the memory.

提交回复
热议问题