第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.