1、typedef的作用是:对原来的数据类型,再起一个名字。注意,原来的数据类型名还是可以使用的。
方式一:不使用typedef:
int i = 10;
方式二:使用typedef:
typedef int mycnt;
mycnt i = 10;
注意一:方式一和方式二是等价的;
注意二:虽然我们在方式二中为int起了个新名字“mycnt”,但我们还是仍然可以继续使用"int i = 10;"语句的。
2、结构体使用typedef(从而使得代码更简洁)
2.1
typedef struct Student
{
int sid;
char name[100];
char sex;
} STU; //给struct Student 起了别名STU,简洁了代码
int main()
{
STU st;//STU st就相当于struct Student st
}
2.2
typedef struct Student
{
int sid;
char name[100];
char sex;
} * PSTU; //PSTU就相当于struct Student *
int main()
{
struct Student st;
PSTU st1 = &st;//PSTU st1就相当于struct Student * st1 ,即st1是一个指针变量
st1->sid = 99;
}
2.3
我们可以把2.1、2.2合起来使用,即
typedef struct Student
{
int sid;
char name[100];
char sex;
}STU,* PSTU;
int main()
{
STU st;
PSTU st1 = &st;
st1->sid = 99;
}