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

Contenu du cours

C Structs

C Structs

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

book
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:

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?

Sélectionnez la réponse correcte

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
toggle bottom row

book
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:

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?

Sélectionnez la réponse correcte

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 3
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
We're sorry to hear that something went wrong. What happened?
some-alt