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

Conteúdo do Curso

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?

Selecione a resposta correta

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 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?

Selecione a resposta correta

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 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?

Selecione a resposta correta

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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?

Selecione a resposta correta

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 3
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt