Unions
A union is a user-defined data type in C, similar to a structure. The main difference is that all of its members share the same memory location. At any given time, only one member can store a value.
Unions are used infrequently, but they are useful when you want a variable that can hold different types of data at different times while saving memory.
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.c
12345678910111213141516#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable a printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); return 0; }
In the example, the variables data.a and data.b share the same memory. We assigned the value 77 to a, but we can interpret the same bytes as a character in b.
Alignment in unions
Like structures, unions also use alignment and padding. The compiler may add extra bytes so that each member starts at the proper memory boundary.
main.c
12345678910111213#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; }
A union can store only one value at a time, and its size is equal to the size of its largest member. In the example with int a (4 bytes), double b (8 bytes), and char c[20], the minimum size of the union is 20 bytes. However, the compiler may add extra bytes for alignment, so that double or int start at the proper memory boundary. As a result, sizeof(union Data) can be larger than 20, for example, 24 bytes.
Swipe to start coding
The union Data stores each value as an int and allows access to the same value as a char. The function should iterate over the array and print each value both as an integer and as a corresponding ASCII character.
The function accepts two parameters: an array of integers (values) and its length (n).
- Use a
union Datawith two members:int aandchar b. - In a
forloop, iterate over the array elements from0ton. - Assign the current element of the array to the
intmember of the union (data.a). - Print the integer value
data.aand the character representationdata.b.
Example
{65, 66, 67, 68, 69} =>
Value as int: 65, Value as char: A
Value as int: 66, Value as char: B
Value as int: 67, Value as char: C
Value as int: 68, Value as char: D
Value as int: 69, Value as char: E
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about how unions save memory compared to structs?
What are some practical examples where using a union is beneficial?
How does alignment and padding affect the size of a union?
Awesome!
Completion rate improved to 4.35
Unions
Swipe to show menu
A union is a user-defined data type in C, similar to a structure. The main difference is that all of its members share the same memory location. At any given time, only one member can store a value.
Unions are used infrequently, but they are useful when you want a variable that can hold different types of data at different times while saving memory.
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.c
12345678910111213141516#include <stdio.h> // declaring a union union Data { int a; char b; } data; int main() { data.a = 77; // assigning a value to variable a printf("Value of data.a: %d\n", data.a); printf("Value of data.b: %c\n", data.b); return 0; }
In the example, the variables data.a and data.b share the same memory. We assigned the value 77 to a, but we can interpret the same bytes as a character in b.
Alignment in unions
Like structures, unions also use alignment and padding. The compiler may add extra bytes so that each member starts at the proper memory boundary.
main.c
12345678910111213#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; }
A union can store only one value at a time, and its size is equal to the size of its largest member. In the example with int a (4 bytes), double b (8 bytes), and char c[20], the minimum size of the union is 20 bytes. However, the compiler may add extra bytes for alignment, so that double or int start at the proper memory boundary. As a result, sizeof(union Data) can be larger than 20, for example, 24 bytes.
Swipe to start coding
The union Data stores each value as an int and allows access to the same value as a char. The function should iterate over the array and print each value both as an integer and as a corresponding ASCII character.
The function accepts two parameters: an array of integers (values) and its length (n).
- Use a
union Datawith two members:int aandchar b. - In a
forloop, iterate over the array elements from0ton. - Assign the current element of the array to the
intmember of the union (data.a). - Print the integer value
data.aand the character representationdata.b.
Example
{65, 66, 67, 68, 69} =>
Value as int: 65, Value as char: A
Value as int: 66, Value as char: B
Value as int: 67, Value as char: C
Value as int: 68, Value as char: D
Value as int: 69, Value as char: E
Solution
Thanks for your feedback!
single