union的一些问题

kuailexiaoziwqx 2016-10-19 01:49:26

/*为什么
A={“a”} A.l=97
A={“aa”} A.l=24929 怎么得来的呢
A={“ab”} A.l=25185 与上一个 数值差256

*/
#include <stdio.h>
#include<iostream>
using namespace std;
union a
{
char str[4];
int l;
}A={"a"};
int main()
{ cout<<A.l;
system("pause");
return 0;
}


...全文
172 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-10-20
  • 打赏
  • 举报
回复
再供参考:
void HexDump(char *buf,int len,int addr) {
    int i,j,k;
    char binstr[80];

    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%08x -",i+addr);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            printf("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        printf("%s\n",binstr);
    }
}
赵4老师 2016-10-20
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <stdlib.h>
char buf[17];
union U {
  unsigned short int aa;
  struct {
    unsigned int bb:7;//(bit 0-6)
    unsigned int cc:6;//(bit 7-12)
    unsigned int dd:3;//(bit 13-15)
  };
} u;
void main() {
                //bbbbbbbbbbbbbbbb
                //iiiiiiiiiiiiiiii
                //tttttttttttttttt
                //111111
                //5432109876543210
                //::::::::::::::::
    u.aa=0xE07F;//1110000001111111
    printf("bb==%d,cc==%d,dd==%d\n",u.bb,u.cc,u.dd);
    u.bb=0x41;
    u.cc=0x21;//dddccccccbbbbbbb
    u.dd=5;   //1011000011000001
    printf("aa==0x%04X==%016s(2)\n",u.aa,itoa(u.aa,buf,2));
}
//bb==127,cc==0,dd==7
//aa==0xB0C1==1011000011000001(2)

xskxzr 2016-10-20
  • 打赏
  • 举报
回复
见http://en.cppreference.com/w/cpp/language/union(标红是我加的重点):
引用
The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined, and it's undefined behavior to read from the member of the union that wasn't most recently written. Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.
楼上一群人一本正经地研究非标准扩展,真的好吗?
窗外蓝天 2016-10-19
  • 打赏
  • 举报
回复
A={"a"} 0-----------32 61 00 00 00,l=0x00000061 A={"aa"} 0-----------32 61 61 00 00,l=0x00006161 A={"ab"} 0-----------32 61 62 00 00,l=0x00006261
Sniper_Pan 2016-10-19
  • 打赏
  • 举报
回复
union占内存为最大变量所占空间,本段代码中X86情况下通常为4字节。 当A={"a"}时,内部存储a的ASCII值,0x61,十进制显示后为97 当A={"aa"}时,内部存储值为0x6161,十进制显示后为24929 当A={“ab”}时,内部存储值为0x6261(小端字节序),十进制显示后为25185

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧