#include <stdio.h>
struct turtle
{
/* data */
char* name;
char* species;
int age;
};
void happ(struct turtle t){
// 函数内部得到的是t的副本
t.age = t.age + 1;
}
void happyP(struct turtle* t){
// 将struct变量的指针传入函数,通过指针修改struct属性
(*t).age = (*t).age + 1;
}
void happyPP(struct turtle* t){
// 将struct变量的指针传入函数,通过指针修改struct属性
// C语言引入了一个新的箭头运算,可从struct指针上获取属性
t->age = t->age + 1;
}
/*
总结:struct变量名,使用点运算符(.)获取属性
struct变量指针,使用箭头运算符(->)获取属性
t == &t
t.age == (*t).age == t->age
*/
int main(void)
{
struct turtle myTurtle = {"tu", "see aka", 212};
happ(myTurtle);
printf("aka turtle's age is %d\n", myTurtle.age); // 212
happyP(&myTurtle);
printf("aka turtle's age is %d\n", myTurtle.age); // 213
return 0;
}