Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende 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.

Tarea

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.

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you show me the code for the ASCIItable example?

How do I access the members of an anonymous union or struct in C?

What are some other practical uses for anonymous structs and unions?

close

bookAnonymous Struct and Union

Desliza para mostrar el menú

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3
single

single

some-alt