zoukankan      html  css  js  c++  java
  • union内嵌struct用法

     
    // union内嵌struct用法
    // 众所周知,union为联合体,struct为结构体。下面根据实例谈谈用法
     
    #include <stdio.h>  
    #include <string.h>  
    void main()  
    {   
        union{                    
            char i[6];   
            struct{               
                char first;   
                char second;  
                char third;  
                char fourth;  
                unsigned short five;  
                  }half;   
             }number;  
        strcpy(number.i,"abcdA");  
        printf("%c%c ",number.half.first, number.half.second);  
        printf("%c%c%d ",number.half.third, number.half.fourth,number.half.five);  
    }
    // 输出结果为: 

    图片

    // union中各个变量是共用内存空间。上例中,char数组和结构体共用内存空间。正如程序输出结果
    // char数组赋值,结构体中的变量就会按顺序获得数组中的值。
     
    main()   
        {   
             union{                     
                  int i;   
                  struct{               
                       char first;   
                       char second;   
                       char third;   
                       char fourth;   
                  }half;   
             }number;   
             number.i=0x44434241;           
             printf("%c%c%c%c ", number.half.first,number.half.second,number.half.third, number.half.fourth);   
             number.half.first='a';     
             number.half.second='b';   
             number.half.third='c';   
             number.half.fourth='d';   
             printf("%x ", number.i);   
        }  
    // 输出结果为: 

    图片

    // 从上例结果可以看出: 当给i赋值后, first,second,third,fourth也就相应被赋值; 
    // 当给first,second,third,fourth赋字符后, 这四个字符的ASCII码也被赋值给i。
  • 相关阅读:
    Spring MVC 核心组件详解
    Spring MVC 入门就这一篇
    Spring 事务解决方案
    【UGUI源码分析】Unity遮罩之Mask详细解读
    游戏开发中不同时区下的时间问题
    ARTS第十三周(阅读Tomcat源码)
    Win10 电脑安装.NET低版本提示“这台计算机中已经安装了 .NET Framwork 4.6.2或版本更高的更新”问题
    Dynamics 365 Setup 提示SqlServer 存在
    Dynamics CRM "Verification of prerequisites for Domain Controller promotion failed. Certificate Server is installed."
    Dynamics CRM
  • 原文地址:https://www.cnblogs.com/david-wei0810/p/5953007.html
Copyright © 2011-2022 走看看