1 Commonwealth (Consortium)
union
{
Data type 1 member variable 1;
Data type 2 member variable 2;
data type 3 member variable 3;
};
Union All member variables share the same space.
And if you change any member value later, the previous value will not count.
For example, the following example
#include <stdio.h>
union data
{
char a;
short b;
int c;
};
int main(int argc, const char *argv[])
{
union data data1;
data1.a = 'A';
printf("data1.a = %c\n",data1.a);
data1.b = 100;
printf("data1.b = %d\n",data1.b);
data1.c = 1100;
printf("data1.c = %d\n",data1.c);
return 0;
}
operation result
2 Memory size endianness
1 Memory little endian: memory low address stores low data bits, memory high address stores high data bits (computers are generally little endian)
2 Memory big end: memory low address stores high data bit, memory high address stores low data bit (hardware such as ARM storage is big endian)
***How to judge whether your device is endian? ***
#include <stdio.h> union big_small_d { int bsd1; char bsd2; }; int main(int argc, char const *argv[]) { union big_small_d data; data.bsd1 = 1; if (data.bsd2) { printf("为小端\n"); } else { printf("为大端\n"); } return 0; }