C7-结构体

一、结构体声明
二、结构体定义
三、结构体应用
四、结构体与数组

代码:

// struct 结构体名
// {
//   类型说明符 成员名;
//   ... ...
//   类型说明符 成员名;
// };

//typedef struct student
//{
// short num;
// char name[20];
// char sex;
// float score;
//} a;

//typedef struct rectangle
//{
// int len;
// int wid;
//} a;

typedef struct student
{
short age;
char name[20];
float score;
} stu;

int main(int argc, const char * argv[]) {

// 重定义名字
// 关键字 已存在名 新名字

// typedef int apple;

// typedef struct rectangle a;

// //数据类型:struct student
// //变量名:stu
// //初值:{1, “little apple”, ‘m’, 99.8}–>按声明顺序, –>对应声明中的类型
// a stu = {1, “little apple”, ‘m’, 99.8};
//
// //使用结构体变量
// //变量名.成员
// stu.num = 59;
// strcpy(stu.name, “abc”);
// printf(“%d\n”, stu.num);
// printf(“%s\n”, stu.name);

// a rec = {3, 4};
// printf(“%d %d “, rec.len, rec.wid);

// apple a = 0;
// int b = 3;
// a = 5;
// printf(“%d %d\n”, a, b);

// stu a = {15, “zhangsan”, 60.00};
// stu b = {16, “lisi”, 70.00};
// stu c = {17, “wangwu”, 80.00};
// //stu temp = {0};
// //temp = a.score > b.score ? a : b;
// //temp = temp.score > c.score ? temp : c;
// //printf(“%d %s %.2f\n”, temp.age, temp.name, temp.score);
// int max = 0;
// float gra = 0;
// max = a.age > b.age ? a.age : b.age;
// max = max > c.age ? max : c.age;
// printf(“最大年龄是:%d\n”, max);
// gra = a.score > b.score ? a.score : b.score;
// gra = gra > c.score ? gra : c.score;
// printf(“最高成绩是:%.2f\n”, gra);

// a st = {0};
// long ret = sizeof(st);
// //计算占内存空间大小
// printf(“%ld\n”, ret);

  // 有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生全部信息

// stu stu1 = {15, “zhangsan”, 60.00};
// stu stu2 = {16, “lisi”, 70.00};
// stu stu3 = {17, “wangwu”, 80.00};
// stu stu4 = {18, “zhaoliu”, 90.00};
// stu stu5 = {19, “qianqi”, 30.00};
// stu temp = {0};
// temp = stu1.score > stu2.score ? stu1 : stu2;
// temp = temp.score > stu3.score ? temp : stu3;
// temp = temp.score > stu4.score ? temp : stu4;
// temp = temp.score > stu5.score ? temp : stu5;
// printf(“%d %s %.2f\n”, temp.age, temp.name, temp.score);
// stu gra[5] = {stu1, stu2, stu3, stu4, stu5};
// stu stutemp = {0};
// for (int i = 0; i < 4; i++) {
// for (int j = 0; j < 4 - i; j++) {
// if (gra[j].score < gra[j + 1].score) {
// stutemp = gra[j];
// gra[j] = gra[j + 1];
// gra[j + 1] = stutemp;
// }
// }
// }
// for (int i = 0; i < 5; i++) {
// printf(“%d %s %.2f\n”, gra[i].age, gra[i].name, gra[i].score);
// }

//结构体数组定义并初始化

// Student stu[3] = {
// {15, “zhangsan1”, 61.00};
// {16, “zhangsan2”, 62.00};
// {17, “zhangsan3”, 63.00};
// };
// //访问结构体数组中的元素/成员
// stu[1].score = 99.9;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值