关于union的两道题目 内存分配

gale98a 2004-09-25 12:14:03
program1
#include"stdio.h"
main()
{
union abc
{
char c[2];
int x;
}s;
s.x=3234;

printf("%d\n",s.c[0]);
printf("%d\n",s.c[1]);
}

program2
#include"stdio.h"
union pp
{
int i;
char ch[2];
}a;
main()
{
a.ch[0]='13';
a.ch[1]='0';

printf("%d\n",a.i);
}

分析打印结果
...全文
195 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lall 2004-09-25
  • 打赏
  • 举报
回复
to 1

x=0000 1100 ,1010 0010
s.c[0]=1010 0010 =-94
s.c[1]=0000 1100 =12
lall 2004-09-25
  • 打赏
  • 举报
回复
编译只是警告
系统会取舍去后面的字符 3
herryhuang 2004-09-25
  • 打赏
  • 举报
回复
1:

162
12

2:

编译错误:a.ch[0]='13';怎么能在单引号中包括两个字符呢?




双杯献酒 2004-09-25
  • 打赏
  • 举报
回复
// C++标准规定:
// 字符常量的范围是: 0x00000000 ---- 0xffffffff
// 对于字符数据类型,
// char是: 0x00--0xff;
// wchar_t是: 0x0000 ---- 0xffff

// VC 6.0
#include <cstdio>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
int a = '1';
int b = '12';
int c = '123';
int d = '1234';
printf("a = 0x%08X\n",a);
printf("b = 0x%08X\n",b);
printf("c = 0x%08X\n",c);
printf("d = 0x%08X\n",d);
system("PAUSE");
return 0;
}
/*
a = 0x00000031
b = 0x00003132
c = 0x00313233
d = 0x31323334
请按任意键继续 . . .
*/
gale98a 2004-09-25
  • 打赏
  • 举报
回复
to lall(); Girl my= new Girl(single); my.find(beijing)

第二题的备选答案是:13 14 208 209

我在VC下的得到的答案跟你一样
gale98a 2004-09-25
  • 打赏
  • 举报
回复
to herryhuang(Herry) ( )

编译可以通过的

好像最多可以包含四个字符的

谁来解释一下 ?

从右往左传递?

lall 2004-09-25
  • 打赏
  • 举报
回复
to 2

a.ch[0]=3=51=0011 0011
a.ch[1]=0=48=0011 0000
a.i=0011 0000 0011 0011 =12339

最近找工作郁闷
给我分了
:)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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