0
点赞
收藏
分享

微信扫一扫

自定义数据类型

流计算Alink 2022-04-25 阅读 135
c++c语言

一、结构体

把一些相关的变量结合在一起,以一个整体进行描述。

结构体的声明:

struct student //名字
{
    定义的变量;  //结构体成员
    int id;
    char name[20];
    float score;
}; //可在 } 和 ;之间直接定义变量

struct student
{
}a1,a2;
int main()
{
    struct student a; //数据类型  变量名
}

结构体变量的访问

//   .   变量名.成员名   例如 a.id;

变量的初始化:

int main()
{
    stuct student c = {99,"zhangfei", 52};  //按照顺序初始化。中间不可省略,未初始化的默认为0
}

/ ************************** typedef 的使用**********************/

//方式一: typedef 类型 类型的别名

typedef struct student STU;  

//方式二:在声明结构体类型时,取别名

typedef struct 标识符
{
    成员列表
}别名;

结构体数组

struct student arr[4];
struct student arr1[4] = {
    {1,"张飞",100 },
    {2,"曹操", 99},
    {3,"刘备", 88},
    {4,"关羽", 89}
}

二、共用体 (关键字 union)

用相同的内存单元存放不同的变量。

union data
{
    int id;
    char c[5];
};

三、枚举类型

//enum  名字{枚举列表}
enum weekday{sum,mon,tue,wed,,thu,fri,sat};
//枚举元素是常量 默认有值,从零开始,依次加一

规定大小和int一样大 

 

举报

相关推荐

0 条评论