马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
char * getGender(int);
typedef struct {
int year;
int month;
int day;
} date;
typedef enum {male, female} gender;
typedef struct {
char *name;
gender sex;
int age;
date birthday;
} student [];
int main(int argc, char *argv[]) {
student s = {
{"张三", male, 29, {1984, 1, 26}},
{"李四", male, 26, {1986, 4, 5}},
{"王五", male, 35, {1973, 8, 5}},
{"如花", female, 30, {1982, 5, 30}}
};
int length = sizeof(s) / sizeof(s[0]);
puts("database is:");
for( int i = 0; i < length; i++ ) {
printf("姓名:%s\t性别:%s\t年龄:%d\t生日:%d-%d-%d\n", s[i].name, getGender(s[i].sex), s[i].age, s[i].birthday.year, s[i].birthday.month, s[i].birthday.day);
}
return 0;
}
char * getGender(int sex) {
switch(sex) {
case 0:
return "男"; break;
case 1:
return "女"; break;
default:
return "人妖";
}
}
请问枚举值只能是int的,我要保存其它数据就保存不了了,这里的性别我用了枚举,还得写个还原数据的函数来处理,有没有其它更方便的办法呢? |