C语言union中包含struct 小结
2012-11-20 21:32
281 查看
一
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.bit.p=0x2;
jin.i=0x12345678;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%d\n",sizeof(jin));
}
编译:
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
56
34
12
8
证明了我的编译器是小端模式。
二
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.i=0x12345678; //与一互换
jin.bit.p=0x2;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%d\n",sizeof(jin));
}
编译:
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
2
34
12
8
与一相比,56变成了2.
三
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.bit.p=0x2;
jin.i=0x12345678;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%x\n",jin.byte.j);
printf("%d\n",sizeof(jin));
// printf("%x\n",jin.byte.j);
}
多了一行黑体。
编译:
[root@localhost algorithm]# vim jin.c
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
56
34
12
12345678
8
打印多了12345678这一行。
若把黑体printf("%x\n",jin.byte.j);改为[b]printf("%x\n",jin.byte.m);则打印的是随意的数字,因为没初始化m,空间只初始化一个int。
[/b]
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.bit.p=0x2;
jin.i=0x12345678;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%d\n",sizeof(jin));
}
编译:
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
56
34
12
8
证明了我的编译器是小端模式。
二
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.i=0x12345678; //与一互换
jin.bit.p=0x2;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%d\n",sizeof(jin));
}
编译:
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
2
34
12
8
与一相比,56变成了2.
三
#include<stdio.h>
int main()
{
union
{
int i;
struct
{
int j;
int m;
}byte;
struct
{
char k;
char p;
char q;
char s;
}bit;
}jin;
jin.bit.p=0x2;
jin.i=0x12345678;
printf("%x\n",jin.bit.k);
printf("%x\n",jin.bit.p);
printf("%x\n",jin.bit.q);
printf("%x\n",jin.bit.s);
printf("%x\n",jin.byte.j);
printf("%d\n",sizeof(jin));
// printf("%x\n",jin.byte.j);
}
多了一行黑体。
编译:
[root@localhost algorithm]# vim jin.c
[root@localhost algorithm]# gcc jin.c -o jin
^[[A[root@localhost algorithm]# ./jin
78
56
34
12
12345678
8
打印多了12345678这一行。
若把黑体printf("%x\n",jin.byte.j);改为[b]printf("%x\n",jin.byte.m);则打印的是随意的数字,因为没初始化m,空间只初始化一个int。
[/b]
相关文章推荐
- [转载] c语言中的结构(struct)和联合(union)简介
- 面试珠玑 C语言中union与struct的区别及sizeof的计算方式
- C语言中的struct、union、enum、Typedef
- C语言struct和union构建ARGB颜色通道
- c语言中的结构(struct)和联合(union)简介
- c语言中的结构(struct)和联合(union)简介
- c语言中的结构(struct)和联合(union)简介
- C语言sizeof(struct)与sizeof(union)
- C语言中的sizeof(struct )和sizeof(union)
- c语言中的结构(struct)和联合(union)简介
- JAVA 与C语言中struct union 字节流转换方案 Javolution
- c语言中的结构(struct)和联合(union)
- C语言,struct,union
- C语言中struct和union的区别是什么
- c语言 结构(struct)和联合(union)简介
- C语言中,struct与union的区别
- C_C语言的enum、struct、union的使用详解
- C++ union/struct使用小结
- C语言struct和union结合使用,空间最小
- c语言中的结构(struct)和联合(union)简介