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

Swipe um das Menü anzuzeigen

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:

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 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.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; }
question mark

How much memory will this union take?

Select the correct answer

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

Awesome!

Completion rate improved to 4.17

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:

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 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.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; }
question mark

How much memory will this union take?

Select the correct answer

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 4.17

Swipe um das Menü anzuzeigen

some-alt