结构体简单应用举例。
- #include <stdio.h>
- struct student {
- int mid;
- int final;
- int hmws;
- };
- void main(void){
- struct student sam = {85, 90, 88};
- struct student tom = {93, 88, 91};
- struct student *he = &tom; // 通过指针访问结构体及其成员
- // 我们不能像读写变量一样读写 student 结构体
- // 不能直接对结构体进行比较,例如 sam == tom 是不对的
- // 但是可以对已经定义的结构体变量进行交换,如下所示:
- tom = sam;
- // 通过输出成员的方式输出 tom
- printf("tom = {%d, %d, %d}\n", tom.mid, tom.final, tom.hmws);
- // 注意点号
- // 输出结果为 tom = {85, 90, 88}
- printf("*he = {%d, %d, %d}\n", he->mid, he->final, he->hmws);
- // 注意箭头
- // 输出结果为 *he = {85, 90, 88}
- // 通过其他方式也可以获得同样的输出结果,如下所示:
- printf("*he = {%d, %d, %d}\n", (*he).mid, (*he).final, (*he).hmws);
- }
输出结果: