1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h>
#include <stdlib.h>
// struct Element { // BAD - wastes spaces
// int as_int;
// char* as_string;
// Node* as_list;
// float as_float;
// };
union Element { // shares the same space in memory for *ONE* of the fields
int as_int; // 4 bytes (on our platform)
char* as_string; // 8 bytes (" " " )
float as_float; // 4 bytes (" " " )
// WHOLE: 8 bytes (max of 4, 8 ,4)
};
int main(int argc, char* argv[]) {
union Element element = { .as_int = 5 };
printf("element.as_int == %d\n", element.as_int);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2019 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.