首先定义一个正常的结构体:
struct 名字
{
xxxxxx
};
定义结构体变量
struct 名字(类型) 名字(变量名)
struct student
{
char name[20];
};
定义结构体变量
struct student s1;
s1.name=xxxxx;
用typedef来定义结构体并进行重命名:
typedef struct student(类型名)
{
char name[20];
}student_t(typedef重命名的类型名,可以与类型名相同);
使用时
student_t s2;
struct student s3;
前面的文章中说过,传值与传址的区别,以及为什么要进行传地的原因,是为了减少内存开销以及提高代码运行效率。
在使用struct定义结构体时,申请结构体指针方式为:
struct student *p1;
那么能不能使得结构体指针定义更方便更简洁呢,答案是,使用typedef:
typedef struct teacher
{
char name[20];
int age;
}teacher(重命名为teacher),*PTeacher(将struct teacher*重命名为PTeacher);
使用方式:
student t1;
t1.age=20;
PTeacher p1=&t1;
printf("teacher age=%d",p1->age);