C语言第74课结构体的初始化及内容的打印

第74课结构体的初始化与内容的打印

·function:直接将已有的游戏信息打印出来

·程序部分

//Hero.harderr**********************************************************
							#ifndef HERO_H_INCLUDED
							#define HERO_H_INCLUDED
							
							//定义结构体
							typedef struct _myTime
							{
								int year;   int month;  int day;
							}MyTime;
							
							typedef struct _hero
							{
								char name[50];      //英雄名称
								char sex;           //英雄性别
								char job[20];       //英雄职业
								int life;           //英雄生命值
								double speed;       //攻击速度
								char ability[20];   //英雄的特殊能力
								MyTime pubTime;     //英雄的上线时间
							
							}Hero;
							
							//函数声明
							void Show();    //显示英雄详细信息
							
							#endif // HERO_H_INCLUDED
							
//Hero.harderr**********************************************************
//Hero.c****************************************************************
							#include "Hero.h"
							
							Hero heroes[] = {
								{"影流之主劫",'m',"刺客",579,0.644,"位移",{2012,8,15}},
								{"琴瑟仙女唢呐",'f',"法师",666,0.644,"减速、治疗",{2010,9,20}},
								{"疾风剑豪",'f',"战士",517,0.67,"护盾、位移",{2013,12,23}}
							};
							//函数--------------------------显示英雄详细信息
							void Show()
							{
								int i;
								//如何知道结构数组的大小呢?
								int len = sizeof(heroes) / sizeof(Hero);    //一共占有的内存数/单个元素占有的内存数 = 元素个数
							// printf("结构数组的元素个数:%d\n", len);
								for (i = 0; i < len; i++)
								{
									printf("%s\t%s\t%d-%d-%d\n",heroes[i].name,heroes[i].job,heroes[i].pubTime.year,heroes[i].pubTime.month,heroes[i].pubTime.day);
								}
							}
							
//Hero.c****************************************************************
//Main.c****************************************************************
							#include <stdio.h>
							#include <stdlib.h>
							#include "Hero.h"
							
							extern Hero heroes[100];
							int main()
							{
								Show();
								return 0;
							}
							
//Main.c****************************************************************

·运行结果

							影流之主劫      刺客    2012-8-15
							琴瑟仙女唢呐    法师    2010-9-20
							疾风剑豪        战士    2013-12-23
							
							Process returned 0 (0x0)   execution time : 0.386 s
							Press any key to continue.
							

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值