Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Anonymous Struct and Union | Advanced Structs Usage
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

bookAnonymous Struct and Union

The C programming language has anonymous structures and unions, which provide the ability to define structures and unions without specifying a name.

Since there are no names, direct objects (or variables) are not created from them we use them as nested structures or unions.

You can use anonymous structs/unions when they are nested and will only be used where they are declared.

c

main

copy
123456789101112131415161718192021222324
#include <stdio.h> struct ASCIItable { // Anonymous union union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // number 65 is first symbol `A`, // number 97 is last `Z` symbol, hence 97 - 65 = 25, // so 26 iterations required (including number 0) for (int i = 0; i < 26; i++) printf("x.num = %d | x.symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); return 0; }

Note

Union allows the same set of bits to be represented differently depending on the context. For example, in an ASCIItable structure, you can interpret the same value as a character (using symbol) or as a numeric value (using num). This can be useful when you need to quickly switch between different data views.

Tarefa

  1. Create an anonymous structure;
  2. Declare three float fields in it with the names x, y, radius;
  3. Define the fields of the structure instance;
  4. Display the values of the structure fields.

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 4. Capítulo 3
toggle bottom row

bookAnonymous Struct and Union

The C programming language has anonymous structures and unions, which provide the ability to define structures and unions without specifying a name.

Since there are no names, direct objects (or variables) are not created from them we use them as nested structures or unions.

You can use anonymous structs/unions when they are nested and will only be used where they are declared.

c

main

copy
123456789101112131415161718192021222324
#include <stdio.h> struct ASCIItable { // Anonymous union union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // number 65 is first symbol `A`, // number 97 is last `Z` symbol, hence 97 - 65 = 25, // so 26 iterations required (including number 0) for (int i = 0; i < 26; i++) printf("x.num = %d | x.symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); return 0; }

Note

Union allows the same set of bits to be represented differently depending on the context. For example, in an ASCIItable structure, you can interpret the same value as a character (using symbol) or as a numeric value (using num). This can be useful when you need to quickly switch between different data views.

Tarefa

  1. Create an anonymous structure;
  2. Declare three float fields in it with the names x, y, radius;
  3. Define the fields of the structure instance;
  4. Display the values of the structure fields.

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 4. Capítulo 3
toggle bottom row

bookAnonymous Struct and Union

The C programming language has anonymous structures and unions, which provide the ability to define structures and unions without specifying a name.

Since there are no names, direct objects (or variables) are not created from them we use them as nested structures or unions.

You can use anonymous structs/unions when they are nested and will only be used where they are declared.

c

main

copy
123456789101112131415161718192021222324
#include <stdio.h> struct ASCIItable { // Anonymous union union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // number 65 is first symbol `A`, // number 97 is last `Z` symbol, hence 97 - 65 = 25, // so 26 iterations required (including number 0) for (int i = 0; i < 26; i++) printf("x.num = %d | x.symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); return 0; }

Note

Union allows the same set of bits to be represented differently depending on the context. For example, in an ASCIItable structure, you can interpret the same value as a character (using symbol) or as a numeric value (using num). This can be useful when you need to quickly switch between different data views.

Tarefa

  1. Create an anonymous structure;
  2. Declare three float fields in it with the names x, y, radius;
  3. Define the fields of the structure instance;
  4. Display the values of the structure fields.

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!

The C programming language has anonymous structures and unions, which provide the ability to define structures and unions without specifying a name.

Since there are no names, direct objects (or variables) are not created from them we use them as nested structures or unions.

You can use anonymous structs/unions when they are nested and will only be used where they are declared.

c

main

copy
123456789101112131415161718192021222324
#include <stdio.h> struct ASCIItable { // Anonymous union union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // number 65 is first symbol `A`, // number 97 is last `Z` symbol, hence 97 - 65 = 25, // so 26 iterations required (including number 0) for (int i = 0; i < 26; i++) printf("x.num = %d | x.symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); return 0; }

Note

Union allows the same set of bits to be represented differently depending on the context. For example, in an ASCIItable structure, you can interpret the same value as a character (using symbol) or as a numeric value (using num). This can be useful when you need to quickly switch between different data views.

Tarefa

  1. Create an anonymous structure;
  2. Declare three float fields in it with the names x, y, radius;
  3. Define the fields of the structure instance;
  4. Display the values of the structure fields.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 4. 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