zoukankan      html  css  js  c++  java
  • struct/enum/union加typedef与不加typedef

    匿名结构体

    struct {
        int a;
        int b;  
    } v;
    
    // 这里表示定义了一个结构体的变量v,且结构体类型没有名字,后续不能再定义结构体,除非又把结构体写一遍。

    有名字的结构体

    struct struct_type{
        int a;
        int b;  
    } v;
    
    // 这里表示定义了一个结构体的变量v,且结构体类型的名字为 "struct struct_type"
    // 后续要定义结构体变量的时候要使用"struct struct_type"
    // 有的编译器使用struct_type定义变量也是可以的,class一样。
    struct struct_type b; // 定义结构体变量b

    使用typedef命名

    typedef struct {
        int a;
        int b;  
    } S_TYPE;
    // 或者
    typedef struct struct_type {
        int a;
        inb b;      
    } S_TYPE;
    
    // 两种定义方式都可以使用"S_TYPE"定义结构体变量,第二中还可以使用"struct struct_type"或者sturct_type定义结构体变量。
    // 使用了typedef就不能在定义类型的时候也声明变量。
    
    S_TYPE v;

    特殊的emue,emue中定义的值其实就是define

    typedef emue {
        RED, BLUE,GREEN  //域之间是用“,”分开,不像struct和union是用“;”分开。
    } color;
    // 等效于
    // #define RED    0
    // #define BLUE   1
    // #define GREEN  2
    
    
    color c = RED;  // 由于域是define的,也就是全局的,可以直接使用

    联合union(就是共享空间的结构体,结构体中每个域都分配空间,而联合体中每个域是共享空间的)

    typedef union {
      int i;                    // i和f是共享空间的
      float f;    
    } num;
    
    num.f = 100;         // 给i和f的共享空间赋值了
    print("%0d, %0f,", num.i, num.f);  // 输出1120403456, 100.0000,整型的值,就是浮点值的二进制对应的值。

    x

  • 相关阅读:
    操作系统与进程.md
    解决粘包现象
    Python3网络爬虫开发实战
    Django学习目录
    前端学习目录
    MySQL数据库学习目录
    第一章 开发环境配置
    15.3 Scrapyd 对接 Docker
    13.4 Spider 的用法
    9.1 代理的设置
  • 原文地址:https://www.cnblogs.com/yuandonghua/p/11490574.html
Copyright © 2011-2022 走看看