• C++基础--结构体声名


    struct是一种数据结构,当需要存储的相关数据为一个集合时,struct是很好的选择;例如,当存储student,学生的学号, 名字,年龄,身高,就构成了一个集合,用stuct声名为:

    typedef struct chemical_student_for_uni{
    char name[32];
    int card_id; int age; int height; }student_c;

    struct指名了这个是chemical_student_for_uni结构,typedef为这个结构起了一个别名student_c, 那么就可以使用student_c来代替struct chemical_student_for_uni的结构

    顺便提一下typedef的用法:

    1. 隐藏数据的类型,例如给类型取别名;

    2. 简化类型定义,同时便于理解;

    struct的存储:struct的成员是按照声名的顺序进行存储的,假设student_c存储在内存中的地址为100, 以上student在内存中的存储形式如下:

     

     struct结构嵌套:

    struct 结构标记{
        类型 成员名;
        类型 成员名;
        struct 结构标记{
            类型 成员名;
            类型 成员名;
            ...
        }结构变量名;
    }结构变量名;

    无结构标记的结构变量:

    struct{
        类型 成员名;
        类型 成员名;
        ...
    }结构变量名;

    以下为结构的例子:

    (1)当无结构标记的结构变量声明,例如student_1的声明:

    struct {/*无结构标记*/
        char name[32];
        int card_id;
        int age;
        int height;  
    }student_1;

    (2)有结构标记的结构变量声明:

    结构定义:

    struct student{
        char name[32];
        int card_id;
        int age;
        int height;  
    };

    结构变量声明:struct student student_1;

    (3)有结构标记的结构变量student_1的声明:

    struct student{
        char name[32];
        int card_id;
        int age;
        int height;  
    }student_1;

    (4)带有typedef的无结构标记的声明:

    定义别名:

    typedef struct {
        char name[32];
        int card_id;
        int age;
        int height;  
    }student_c;

    定义结构变量名:student_c student_1;

    (5)带有typedef的有结构标记的声明:

    定义别名:

    typedef struct student{
        char name[32];
        int card_id;
        int age;
        int height;  
    }student_c;

    当定义时,可以直接使用别名:student_c student_1;

  • 相关阅读:
    rabbitmq无用使用guest用户远程连接
    SpringMVC 过滤器
    Spring MVC的Controller统一异常处理:HandlerExceptionResolver
    springMVC之mvc:interceptors拦截器的用法
    spring中排除某个类
    mysql中的CURRENT_TIMESTAMP
    [QT]Qt+VS2012+Win8 64Bit安装
    UOJ#55. 【WC2014】紫荆花之恋
    CodeChef SADPAIRS:Chef and Sad Pairs
    BZOJ4771: 七彩树
  • 原文地址:https://www.cnblogs.com/anlia/p/5950443.html
Copyright © 2020-2023  润新知