5.离散结构-typedef【郝斌数据结构】
预备知识:typedef
例题1:typedef为已有的数据类型重新起个名字
1 # include <stdio.h> 2 //typedef作用:为已有的数据类型重新起个名字 3 typedef int ZHANGSAN;//为int再重新多取一个名字,ZHANGSAN等价于int 4 typedef struct Student//struct Student 为数据类型 5 { 6 int sid; 7 char name[100]; 8 char sex; 9 }ST; 10 int main(void) 11 { 12 int i=10;//等价于ZHANGSAN i = 10; 13 ZHANGSAN j=20; 14 printf("%d\n", j); 15 struct Student st;//等价于ST st; 16 struct Student * ps = &st;//等价于 ST * ps 17 ps->sid = 300; 18 printf("%d\n", st.sid); 19 ST st2; 20 st2.sid = 200; 21 printf("%d\n", st2.sid); 22 return 0; 23 }
例题2:
1 # include <stdio.h> 2 typedef struct Student 3 { 4 int sid; 5 char name[100]; 6 char sex; 7 }* PST;// PST等价于struct Student * 8 int main(void) 9 { 10 struct Student st; 11 PST ps = &st; 12 ps->sid = 99; 13 printf("%d\n", ps->sid); 14 15 return 0; 16 }
例题3:
1 # include <stdio.h> 2 # include <string.h> 3 typedef struct Student 4 { 5 int sid; 6 char name[100]; 7 char sex; 8 }* PSTU, STU;// 等价于 STU代表了struct Student, PSTU代表了struct Student * 9 int main(void) 10 { 11 STU st;//struct Student st 12 PSTU pst = &st;//struct Student * ps = &st; 13 pst->sid = 99; 14 strcpy(pst->name, "大刀王五"); 15 pst->sex = '2'; 16 printf("%d %s %c\n", pst->sid, pst->name, pst->sex); 17 return 0; 18 }

浙公网安备 33010602011771号