zoukankan      html  css  js  c++  java
  • 共用体和枚举

    共用体:同一时刻只有一个发生

    union 共用体名

    {

      数据类型 成员1;

      数据类型 成员2;

      .......

    };

    定义:

    #include <stdio.h>
    #include <stdlib.h>
    
    union test_un
    {
        int i ;
        float f ;
        double d ;
        char ch ;
    };
    
    int main()
    {
        union test_un a ;
        a.f = 3.13;
        printf("%d
    ",sizeof(a));//打印:8  所有成员共用一块空间
        printf("%f
    ",a.f);
        exit(0);
    }

    ps:所有成员共用同一块空间,只有一个成员是有效的。

    成员引用:1.变量名.成员名

                  2.指针名->成员名

    大小端:大端---数据低位保存到高地址中

               小端---数据低位保存到低地址中

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    /*struct//结构体嵌套共用体 
    {
        int i ; 
        char ch ;
        union
        {
            int a ;
            char c ;
        }un;
    
    };
    
    union//共用体嵌套结构体
    {
        int a ;
        double b ;
        struct 
        {   
            int arr[10];
            float f ;
        }c;
    };*/
    //4个字节
    union
    {
        struct
        {
            uint16_t i ;
            uint16_t j ;
        }x;
        uint32_t y ;
    }a;
    
    //unsigned int 的数据低16位和高16位相加,结果4466
    int main()
    {
        //uint32_t i = 0x11223344;
        //printf("%x
    ",((i >> 16 ) + i & 0xffff));
        a.y = 0x11223344 ;
        printf("%x
    ",a.x.i+a.x.j);
        exit(0);
    }

    枚举类型:集合

    enum 标识符

    {

      成员1,

      成员2,

      ......

    };

    #include <stdio.h>
    #include <stdlib.h>
    
    enum day 
    {
            MON,
            TUS,
            WEN,
            THR,
            FRI,
            SAT,
            SUN 
        
    };
    
    int main()
    {
            enum day a  = FRI;
    
            printf("%d
    ",a);
            exit(0);
  • 相关阅读:
    真理
    使用C#调用QC的接口
    如何让asp.net应用程序定时自动执行代码
    对话
    科学●哲学●艺术●恶搞
    避免asp.net程序session过期的一个另类方法
    醉翁之意不在酒
    测试团队的新兴职位:测试设计师
    1和0的世界
    名词解释:高阻态,上拉电阻
  • 原文地址:https://www.cnblogs.com/muzihuan/p/5220303.html
Copyright © 2011-2022 走看看