|
发表于 2021-12-16 20:16:22
|
显示全部楼层
- $ cat main.c
- #include <stdio.h>
- void output(const void *ptr, size_t size) {
- for(size_t i = 0; i < size; ++i) {
- printf("%.2x ", ((unsigned char *)ptr)[i]);
- }
- puts("");
- }
- int main() {
- union exa {
- struct {
- int a;
- int b;
- } out;
- int c;
- int d;
- } e;
- output(&e, sizeof(e));
- e.out.a = 10;
- output(&e, sizeof(e));
- e.out.b = 20;
- output(&e, sizeof(e));
- e.c = 2;
- output(&e, sizeof(e));
- e.d = 5;
- output(&e, sizeof(e));
- puts("");
- printf("%d,%d\n", e.out.a, e.out.b);
- return 0;
- }
- $ gcc-debug -o main main.c
- $ ./main
- 78 f1 2d 29 ff 7f 00 00
- 0a 00 00 00 ff 7f 00 00
- 0a 00 00 00 14 00 00 00
- 02 00 00 00 14 00 00 00
- 05 00 00 00 14 00 00 00
- 5,20
- $
复制代码 |
|