【发布时间】:2012-05-05 01:10:38
【问题描述】:
请多多包涵,我来自其他语言,是 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 函数返回一个 struct Person 的指针。我不明白is(可能是因为我来自其他语言,erlang,ruby),为什么将它定义为
struct Person *Person_create(char *name, int age, int height, int weight)
不是
struct Person Person_create(char *name, int age, int height, int weight)
还有其他方法可以定义一个函数来返回一个结构吗?
抱歉,这个问题太基础了。
【问题讨论】:
-
了解
Person *和Person的区别。Person *是指向对象的指针,而Person是对象本身。两者都是不同的类型,例如int *和int的不同之处。 -
所以,
struct Person *Person_create与struct Person * Person_create和struct Person* Person_create相同吗?*位置无所谓? -
是的,
*周围的空格在这里无关紧要。 -
@bighostkim Astericks and Pointers
标签: c