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

Зміст курсу

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

bookUnions

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:

c

main

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

c

main

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; }
How much memory will this union take?

How much memory will this union take?

Виберіть правильну відповідь

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 3
toggle bottom row

bookUnions

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:

c

main

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

c

main

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; }
How much memory will this union take?

How much memory will this union take?

Виберіть правильну відповідь

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 3
toggle bottom row

bookUnions

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:

c

main

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

c

main

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; }
How much memory will this union take?

How much memory will this union take?

Виберіть правильну відповідь

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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:

c

main

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

c

main

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; }
How much memory will this union take?

How much memory will this union take?

Виберіть правильну відповідь

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt