IOS成长中 C语言之结构体

知识点概括:

                1.结构体声明
                2.结构体定义
                3.结构体应用
                4.结构体与数组

结构体所占的空间可以简单的认为是各个成员所占内存空间之和。
实际是最大成员变量所占空间的最小整数倍。

结构体和数组的相互嵌套可以实现比较复杂的数据结构。

一.

<span style="font-size:12px;">#import "structs.h" //导入头文件
//导入自定义的头文件时,使用"",导入系统定义的头文件时,使用<>
/**
 *  结构体是一个自定义的数据类型,也是用来存储多个数据的大容器,只不过结构体要比数据灵活,他可以存储不同类型的数据
 */
//结构体的定义
//struct + 结构体的名字 {大括号中填写结构体成员,多个结构体成员之间通过分好进行间隔} + ;(分号必不可少)
//typedef struct student {
//    int number;   //存储学号
//    char sex;     //存储性别
//} Student; //分号是结构体定义的结束标志,必不可少
//typedef struct point {
//    float x;  //存储横坐标
//    float y;  //存储纵坐标
//} oint;
//typedef struct rectangle {
//    float length; //存储长度
//    float wide;   //存储宽度
//} ect ;
//tyoedef 第一种格式 先定义结构体类型 然后再typedef
//typedef 第二种格式 定义结构体的同时 typedef
//typedef 类型重定义(给已有的类型重新起一个名字), 新的名字和原类型具有同样的功能
//typedef int Integer;
给结构体类型struct student 重新定义一个名字Student
//typedef struct student Student;
//typedef struct point oint;
//typedef struct rectangle ect;</span>
二.

//定义一个学生结构体变量,和定义int char float类型变量一样,只不过把类型换成结构体类而已
    //结构体类型 = struct + 结构体名字
    //struct student 学生结构体类型
    //stu 学生结构体变量的名字
//    Student stu = {9527, 'M'};
//    oint point1 = {12.3, 15.4};
//    ect rect = {12.0, 13.0};
//    //结构体变量访问结构体成员的方式,通过结构体变量,结构体成员
//    stu.number = 100;
//    printf("学号:%d 性别:%c\n", stu.number, stu.sex);
//    printf("x = %.2f y = %.2f\n", point1.x, point1.y);
//    printf("长度:%.2f 宽度:%.2f\n", rect.length, rect.wide);
//    Stu xiaoGuang = {"xiaoGuangguang", 'F', 10};
//    printf("%s %c %d\n", xiaoGuang.name, xiaoGuang.sex, xiaoGuang.age);
//    Stu xiaoMing = {0};
//    xiaoMing = xiaoGuang; //结构体变量可以直接赋值
//    printf("%s %c %d", xiaoMing.name, xiaoMing.sex, xiaoMing.age);
    
    //练习
//    Stu xiaoMing = {"xiaoMing", 19, 71.0};
//    Stu xiaoHong = {"xiaoHong", 18, 65.5};
//    Stu xiaoHei = {"xiaoHei", 20, 88.0};
//    Stu zuiGaoZhe = {0};
//    Stu zuiXiaoZhe = {0};
//    //1.先找出两个人中分数最高者
//    zuiGaoZhe = xiaoMing.score > xiaoHong.score ? xiaoMing : xiaoHong;
//    //2.再找出分数最高者 和 第三个人中的分数最高者
//    zuiGaoZhe = zuiGaoZhe.score > xiaoHei.score ? zuiGaoZhe : xiaoHei;
//    //1.先找出两个人年龄最小者
//    zuiXiaoZhe = xiaoMing.age < xiaoHong.age ? xiaoMing : xiaoHong;
//    //2.再找出年龄最小者 和 第三个人中的年龄最小者
//    zuiXiaoZhe = zuiXiaoZhe.age < xiaoHei.age ? zuiXiaoZhe : xiaoHei;
//    printf("分数最高者:%s %d %.1f\n", zuiGaoZhe.name, zuiGaoZhe.age, zuiGaoZhe.score);
//    printf("年龄最小者:%s %d %.1f\n", zuiXiaoZhe.name, zuiXiaoZhe.age, zuiXiaoZhe.score);
    
    //求数据类型所占内存空间的大小
//    printf("%lu", sizeof(Stu));
    
    //结构体嵌套
//    Stu xiaoGuang = {"xiaoguangguang", 'M', 20, 100, {2014, 7, 24}};
//    Stu xiaoMing = {"xiaomingming", 'M', 20, 75, {2014, 7, 23}};
//    Stu xiaoHong = {"xiaohonghong", 'M', 20, 59, {2014, 7, 22}};
//    printf("%s\n", xiaoGuang.name);
//    printf("xiaoguang的出生日期:%d-%d-%d\n", xiaoGuang.birth.year, xiaoGuang.birth.month, xiaoGuang.birth.day);
//    printf("xiaohong的出生日期:%d-%d-%d\n", xiaoHong.birth.year, xiaoHong.birth.month, xiaoHong.birth.day);
//    printf("xiaoming的出生日期:%d-%d-%d\n", xiaoMing.birth.year, xiaoMing.birth.month, xiaoMing.birth.day);
//    printStudentInfo(xiaoGuang);
//    printStudentInfo(xiaoHong);
//    printStudentInfo(xiaoMing);
    
    //定义一个有5个学生的结构体数组
//    Stu a[5] = {
//        {"xiaomeng", 'M', 38, 52, {2000, 1, 1}},
//        {"xiaoshuai", 'F', 28, 10, {2000, 1, 2}},
//        {"xiaoguang", 'M', 18, 59, {2000, 1, 3}},
//        {"xiaoming", 'F', 19, 70, {2000, 1, 4}},
//        {"xiaohong", 'M', 20, 88, {2000, 1, 5}},
//    };
//    //先获取第四个人
//    a[3];
//    //获取第四个人的年龄
//    a[3].birth.day;
    //1.输出数组中所有学生的信息
//    for (int i = 0; i < 5; i++) {
//        printStudentInfo(a[i]);
//    }
//    printAllStudentInfo(a, 5);
    
    //输出5个人中成绩最高的人信息
//    Stu max = {0};
//    for (int i = 0; i < 5; i++) {
//        max = max.score > a[i].score ? max : a[i];
//    }
//    max = biJiao(a, 5);
//    printStudentInfo(max);
    
    //输出成绩从高到低排序后的信息
//    printf("排序后:\n");
//    maxToMin(a, 5);

三.

//结构体内存分配原则
//以结构体成员所规定的存储空间最大的空间大小为单位来进行分配,是最大空间的最小整数倍

//定义学生结构体
//#pragma pack(1) //以1为单位来进行分配,不建议做
/*
typedef struct birthday {
    int year;  //存储年份
    int month; //存储月份
    int day;   //存储天数
}Birthday;

//结构体嵌套,在一个结构体中的结构体成员是另一个结构体类型的变量

typedef struct stu {
    char name[20]; //存储学生姓名
    char sex;      //存储学生性别
    int age;       //存储学生年龄
    float score;   //存储成绩
    Birthday birth;//出生日期
}Stu;

//输出结构体的结构体成员
void printStudentInfo(Stu student);

//输出结构体数组结构体成员
void printAllStudentInfo(Stu student[], int count);

//比较成绩大小
Stu biJiao(Stu a[], int count);

//成绩从高到低排序
void maxToMin(Stu a[], int count);
*/
四.
/*
void printStudentInfo(Stu student) {
    printf("name:%s  sex:%c  age:%d  score:%.2f  year-month-day:%d-%d-%d\n", student.name, student.sex, student.age, student.score, student.birth.year, student.birth.month, student.birth.day);
}

void printAllStudentInfo(Stu student[], int count) {
    for (int i = 0; i < count; i++) {
        printStudentInfo(student[i]);
    }
}

Stu biJiao(Stu a[], int count) {
    Stu max = {0};
    for (int i = 0; i < count; i++) {
        max = max.score > a[i].score ? max : a[i];
    }
    return max;
}

void maxToMin(Stu a[], int count) {
   Stu temp = {0};
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - 1 - i; j++) {
            if (a[j].score < a[j + 1].score) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    printAllStudentInfo(a, count);
}
*/











































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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值