Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Unions | Understanding Structs and Memory
Mastering C Structs

bookUnions

Note
Definition

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

main.c

copy
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

main.c

copy
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.

Task

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).

  1. Use a union Data with two members: int a and char b.
  2. In a for loop, iterate over the array elements from 0 to n.
  3. Assign the current element of the array to the int member of the union (data.a).
  4. Print the integer value data.a and the character representation data.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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

close

bookUnions

Swipe to show menu

Note
Definition

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

main.c

copy
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

main.c

copy
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.

Task

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).

  1. Use a union Data with two members: int a and char b.
  2. In a for loop, iterate over the array elements from 0 to n.
  3. Assign the current element of the array to the int member of the union (data.a).
  4. Print the integer value data.a and the character representation data.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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
single

single

some-alt