2025年3月18日 星期二 甲辰(龙)年 月十七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

C语言结构体简单应用范例

时间:12-29来源:作者:点击数:99

结构体简单应用举例。

  • #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);
  • }

输出结果:

tom = {85, 90, 88}
*he = {85, 90, 88}
*he = {85, 90, 88}
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门