請多多包涵,我從其他語言和新手m設置爲c和從http://c.learncodethehardway.org/book/learn-c-the-hard-way.html定義一個函數返回結構指針
struct Person {
char *name;
int age;
int height;
int weight;
};
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函數返回結構Person的指針。我不明白的是(可能是因爲其他語言,二郎,紅寶石IM),爲什麼它把它定義爲
struct Person *Person_create(char *name, int age, int height, int weight)
不
,是有其他的方式來定義一個函數返回結構?
對不起,如果這個問題太基本了。
瞭解'Person *'和'Person'之間的區別。 'Person *'是一個指向對象的指針,而'Person'是對象本身。兩者都是不同的類型,例如'int *'和'int'的不同。 – Mahesh
因此,'struct Person * Person_create'與'struct Person * Person_create'和'struct Person * Person_create'是一樣的嗎? '*'的位置並不重要? – allenhwkim
是的,'*'周圍的空白在這裏並不重要。 –