联合的使用方法

    技术2022-05-19  24

    例:

    #include <stdio.h>

    typedef   union tag_Word{    short  ret;    unsigned char ch[2];} Word;  //联合体中的各变量内存地址相同,大小为最大的最大类型的长度

    int diayong(){        Word su;

            su.ch[0] = 17;        su.ch[1] = 27;

            return su.ret;  //可以利用ret类型,返回sh数组中的两个成员(当然可以就只给su.ret成员赋值)}

    Word diyo(){        Word ha;

            ha.ret = diayong();

            return ha;}

    main(){        Word sh;        sh = diyo();        printf("%d/n", sh.ch[1]);        printf("%d/n", sh.ch[0]);

    }

     

     

    打印结果为:

    2717

     

     


    最新回复(0)