Course Content
C Structs
C Structs
Unions
Union is a user-defined data type, just like a structure.
Unions are not often used, but if you want to describe a type that can contain different types at different times, but only one at a time, then you may need a union.
The data is located in the same memory area for all members of the union. This means that all members of the union share the same memory address, and the size of the union is equal to the size of its largest member.
The main use of union is the imposition of different types of data on one common memory area, that is, the representation of one memory area as different data types.
Since in the union the fields are in the same memory cell, we can process the same data in different ways:
main
#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable b printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); // access variable f after assigning value i return 0; }
Note
In unions, as in structures, the mechanism of alignment and filling also works.
The compiler added 4 bytes of padding between the char array str[20]
and the double d
variable so that the double variable would start at the alignment boundary, which could be 8 bytes.
main
#include <stdio.h> union Data { int a; // 4 bytes double b; // 8 bytes char c[20]; // 20 bytes }; int main() { printf("Size of union Data: %zu bytes\n", sizeof(union Data)); return 0; }
Thanks for your feedback!
Unions
Union is a user-defined data type, just like a structure.
Unions are not often used, but if you want to describe a type that can contain different types at different times, but only one at a time, then you may need a union.
The data is located in the same memory area for all members of the union. This means that all members of the union share the same memory address, and the size of the union is equal to the size of its largest member.
The main use of union is the imposition of different types of data on one common memory area, that is, the representation of one memory area as different data types.
Since in the union the fields are in the same memory cell, we can process the same data in different ways:
main
#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable b printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); // access variable f after assigning value i return 0; }
Note
In unions, as in structures, the mechanism of alignment and filling also works.
The compiler added 4 bytes of padding between the char array str[20]
and the double d
variable so that the double variable would start at the alignment boundary, which could be 8 bytes.
main
#include <stdio.h> union Data { int a; // 4 bytes double b; // 8 bytes char c[20]; // 20 bytes }; int main() { printf("Size of union Data: %zu bytes\n", sizeof(union Data)); return 0; }
Thanks for your feedback!
Unions
Union is a user-defined data type, just like a structure.
Unions are not often used, but if you want to describe a type that can contain different types at different times, but only one at a time, then you may need a union.
The data is located in the same memory area for all members of the union. This means that all members of the union share the same memory address, and the size of the union is equal to the size of its largest member.
The main use of union is the imposition of different types of data on one common memory area, that is, the representation of one memory area as different data types.
Since in the union the fields are in the same memory cell, we can process the same data in different ways:
main
#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable b printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); // access variable f after assigning value i return 0; }
Note
In unions, as in structures, the mechanism of alignment and filling also works.
The compiler added 4 bytes of padding between the char array str[20]
and the double d
variable so that the double variable would start at the alignment boundary, which could be 8 bytes.
main
#include <stdio.h> union Data { int a; // 4 bytes double b; // 8 bytes char c[20]; // 20 bytes }; int main() { printf("Size of union Data: %zu bytes\n", sizeof(union Data)); return 0; }
Thanks for your feedback!
Union is a user-defined data type, just like a structure.
Unions are not often used, but if you want to describe a type that can contain different types at different times, but only one at a time, then you may need a union.
The data is located in the same memory area for all members of the union. This means that all members of the union share the same memory address, and the size of the union is equal to the size of its largest member.
The main use of union is the imposition of different types of data on one common memory area, that is, the representation of one memory area as different data types.
Since in the union the fields are in the same memory cell, we can process the same data in different ways:
main
#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable b printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); // access variable f after assigning value i return 0; }
Note
In unions, as in structures, the mechanism of alignment and filling also works.
The compiler added 4 bytes of padding between the char array str[20]
and the double d
variable so that the double variable would start at the alignment boundary, which could be 8 bytes.
main
#include <stdio.h> union Data { int a; // 4 bytes double b; // 8 bytes char c[20]; // 20 bytes }; int main() { printf("Size of union Data: %zu bytes\n", sizeof(union Data)); return 0; }