zoukankan      html  css  js  c++  java
  • OC 结构体

    void test() {
        // 这个机构只能在函数内部使用
        // 定义一个名为Student的结构体类型
        struct Student {
            int age; // 年龄
            char *name; // 姓名
            float height; // 身高
        };
        
        // 定义一个结构体变量
        struct Student stu = {27, "mj", 1.8f};
        // 下面这行的初始化是错误的
        // stu = {27, "mj", 18.0f};
        
        stu.age = 28;
        
        printf("age=%d
    ", stu.age);
        printf("name=%s
    ", stu.name);
        printf("height=%.1f
    ", stu.height);
    }
    
    void test1() {
        struct Student {
            int age; // 年龄
            char *name; // 姓名
            float height; // 身高
        } stu = {27, "mj", 1.8f};
        
        struct Student stu1 = {28, "lmj", 1.9f};
        
        
        struct {
            int age; // 年龄
            char *name; // 姓名
            float height; // 身高
        } stu2 = {27, "mj", 1.8f};
        
        struct {
            int age; // 年龄
            char *name; // 姓名
            float height; // 身高
        } stu3 = {27, "mj", 1.8f};
    }
    
    void test2() {
        // 定义一个Date结构体
        struct Date {
            int year;
            int month;
            int day;
        };
        
        // 定义一个学生结构体
        struct Student {
            int age;
            struct Date birthday;
        };
        
        struct Student stu = {27, {2009, 10, 10}};
        
        printf("age=%d
    ", stu.age);
        printf("year=%d
    ", stu.birthday.year);
        printf("month=%d
    ", stu.birthday.month);
        printf("day=%d
    ", stu.birthday.day);
    }
    
    void test3() {
    //    struct Student {
    //        int age; // 年龄
    //        char *name; // 姓名
    //        float height; // 身高
    //    };
    //    struct Student a[2] = {{27, "mj", 1.8f}, {28, "lmj", 1.9f}};
        
        struct Student {
            int age; // 年龄
            char *name; // 姓名
            float height; // 身高
        } a[2] = {{27, "mj", 1.8f}, {28, "lmj", 1.9f}};
        
        struct Student a2[4];
    }
    
    struct Person {
        int age;
    };
    
    void change(struct Person p) {
        p.age = 9;
    }
    // 结构体作为函数参数
    void test4() {
        struct Person person = {27};
        change(person);
        
        printf("age=%d", person.age);
    }
    
    // 指向结构体的指针
    void tets5() {
        // 定义一个结构体变量
        struct Person person = {27};
        // 定义一个指向结构体的指针
        struct Person *p;
        // 让指针p指向结构体person
        p = &person;
        
        printf("age=%d
    ", person.age);
        printf("age=%d
    ", (*p).age);
        printf("age=%d
    ", p->age);
    }
    
    int main(int argc, const char * argv[])
    {
        tets5();
        return 0;
    }
  • 相关阅读:
    CF991D Bishwock
    CF1010D Mars rover
    NOIP 2011 计算系数
    SDOI 2008 仪仗队
    浅谈欧拉函数
    CF1249F Maximum Weight Subset
    NOIP 2011 铺地毯
    CF707D Persistent Bookcase
    C++ STL bitset 容器详解
    CF798D Mike and distribution
  • 原文地址:https://www.cnblogs.com/liuwj/p/6899611.html
Copyright © 2011-2022 走看看