簡體   English   中英

函數指針和結構

[英]function pointer and struct

我正在閱讀有關c中的結構的內容,並遇到了這段代碼。 我希望有人可以幫助我分解這段代碼,並了解它在做什么。

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;
};

具體來說,這是我不理解的部分代碼

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

因此該函數返回一個指向struct Person的指針。

這是常見的:

 [return type] func([arguments])

如果要編寫函數指針,則將具有:

 [return type] (*func_pointer_name)([arguments])

 struct Person * (*person_create_p)(char *, int, int, int) = &Person_create;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM