Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Anonymous Struct and Union | Advanced Structs Usage
C Structs

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.

What Are Anonymous Structures and Unions?

An anonymous union or anonymous structure does not have an identifier. Because of this, you cannot create standalone variables of that type; instead, they are typically used inside other structures.

// Anonymous union
union {
    char x;
    int y;
};

// Anonymous structure
struct {
    char x;
    int y;
};

Anonymous structs and unions are especially useful when they are nested and will only be used in the context where they are declared.

Practical Example

Consider a scenario where you want to represent an ASCII table where the same value can be interpreted as a numeric code or a character symbol. Using an anonymous union inside a structure allows you to switch between these two interpretations efficiently.

main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> struct ASCIItable { // Anonymous union allows dual representation of the same memory union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // ASCII code for 'A' // Print all uppercase letters and their numeric codes for (int i = 0; i < 26; i++) { printf("num = %d | symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); } return 0; }

In this example, the structure ASCIItable contains an anonymous union. The union allows the same memory to be interpreted either as a numeric value (num) or as a character (symbol).

When AZtable.num is set to 65, it corresponds to the ASCII code for 'A'. Using the same memory, AZtable.symbol also represents 'A'. By iterating 26 times, we can print all uppercase letters along with their ASCII codes, demonstrating how the same memory can represent multiple views of the data.

Tarefa

Swipe to start coding

Create a function findFurthestPoint that finds the point which is the furthest from the origin (0,0) in a 2D plane. Each point is represented by a structure Point containing an anonymous nested structure with fields x and y.

The function should calculate the Euclidean distance for each point using the formula:

distance = sqrt(x*x + y*y)

and return the index of the point with the maximum distance.

  1. Inside the function, calculate the distance of the first point from the origin using formula.
  2. Initialize maxIndex to 0.
  3. Use a for loop from i = 1 to i = n to iterate through the points.
  4. For each point, calculate its distance using the formula above.
  5. If the distance is greater than maxDistance, update maxDistance and maxIndex to the current point's distance and index.
  6. After the loop, return maxIndex as the index of the furthest point.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 3
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookAnonymous Struct and Union

Deslize para mostrar o menu

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

What Are Anonymous Structures and Unions?

An anonymous union or anonymous structure does not have an identifier. Because of this, you cannot create standalone variables of that type; instead, they are typically used inside other structures.

// Anonymous union
union {
    char x;
    int y;
};

// Anonymous structure
struct {
    char x;
    int y;
};

Anonymous structs and unions are especially useful when they are nested and will only be used in the context where they are declared.

Practical Example

Consider a scenario where you want to represent an ASCII table where the same value can be interpreted as a numeric code or a character symbol. Using an anonymous union inside a structure allows you to switch between these two interpretations efficiently.

main.c

main.c

copy
123456789101112131415161718192021
#include <stdio.h> struct ASCIItable { // Anonymous union allows dual representation of the same memory union { char symbol; int num; }; }; int main() { struct ASCIItable AZtable; AZtable.num = 65; // ASCII code for 'A' // Print all uppercase letters and their numeric codes for (int i = 0; i < 26; i++) { printf("num = %d | symbol = `%c`\n", AZtable.num + i, AZtable.symbol + i); } return 0; }

In this example, the structure ASCIItable contains an anonymous union. The union allows the same memory to be interpreted either as a numeric value (num) or as a character (symbol).

When AZtable.num is set to 65, it corresponds to the ASCII code for 'A'. Using the same memory, AZtable.symbol also represents 'A'. By iterating 26 times, we can print all uppercase letters along with their ASCII codes, demonstrating how the same memory can represent multiple views of the data.

Tarefa

Swipe to start coding

Create a function findFurthestPoint that finds the point which is the furthest from the origin (0,0) in a 2D plane. Each point is represented by a structure Point containing an anonymous nested structure with fields x and y.

The function should calculate the Euclidean distance for each point using the formula:

distance = sqrt(x*x + y*y)

and return the index of the point with the maximum distance.

  1. Inside the function, calculate the distance of the first point from the origin using formula.
  2. Initialize maxIndex to 0.
  3. Use a for loop from i = 1 to i = n to iterate through the points.
  4. For each point, calculate its distance using the formula above.
  5. If the distance is greater than maxDistance, update maxDistance and maxIndex to the current point's distance and index.
  6. After the loop, return maxIndex as the index of the furthest point.

Solução

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
single

single

some-alt