• C Programming Video Tutorials

Anonymous Structures and Unions in C



The feature of anonymous structures and unions was introduced in C11 standard. The aim was to enhance the flexibility of C and also to discard the need of superfluous naming in certain cases.

The feature of defining struct and union anonymously has been found to be extremely useful, especially in applications involving the creation of complex data structures, hardware register mappings, etc. This allows for more straightforward and readable code.

Anonymous Structure

An anonymous structure is a struct definition without a tag or a typedef. It is usually nested within another struct.

Syntax for Anonymous Struct

Here is the syntax of defining an anonymous structure −

struct type {
   elem1;
   elem2;
   struct {
      elem3;
      elem4;
   };
};

Members of an anonymous struct/union can access the parent struct directly, simplifying the notation.

Example 1

In the code below, we have defined a structure with the name as employee. Inside it, an anonymous struct is intended to hold date, month and year of birth of the employee. In the example on nested structure, we had an internal dob structure. Now we’ll use anonymous struct.

#include <stdio.h>

struct employee {
   char name[10];
   float salary;
   struct {
      int d, m, y;
   };
};

int main(){

   struct employee e1;
   strcpy (e1.name, "Kiran");
   e1.salary=25000;
   e1.d = 12;
   e1.m = 5;
   e1.y = 1990;

   printf("Name: %s\n", e1.name);
   printf("Salary: %f\n", e1.salary);
   printf("Date of Birth: %d-%d-%d\n", e1.d, e1.m, e1.y);
   
   return 0;
}

Output

When you run this code, it will produce the following output −

Name: Kiran
Salary: 25000.000000
Date of Birth: 12-5-1990

Note that the "d", "m" and "y" elements of inner anonymous struct are accessed directly.

Example 2

The outer struct type in the following example is student, which has a nested anonymous struct to store the title and ID of the course.

#include <stdio.h>

struct student {
   char name[10];
   int age;
   struct {
      char coursettl[20];
      int courseid;
   };
};

int main(){

   struct student s1;
   strcpy (s1.name, "Kiran");
   s1.age = 27;
   strcpy(s1.coursettl, "C Programming");
   s1.courseid=1;

   printf("Name: %s\n", s1.name);
   printf("age: %d\n", s1.age);
   printf("Course Title: %s Course ID: %d\n", s1.coursettl, s1.courseid);

   return 0;
}

Output

When you run this code, it will produce the following output −

Name: Kiran
age: 27
Course Title: C Programming Course ID: 1

Anonymous Union

An anonymous union is a special type of union that doesn't have a name. Unlike regular unions, anonymous unions are primarily used to create unnamed members that can be accessed directly without qualifying them with a union name.

Syntax for Anonymous Union

Here is the syntax of defining an anonymous union −

struct type {
   elem1;
   elem2;
   union {
      elem3;
      elem4;
   };
};

Example

Anonymous unions do not have a name. The elements share the same memory location. The members of the anonymous union can be accessed directly without using a union name.

Take a look at the following example −

#include <stdio.h>

struct mystruct {
   int var;
   union {
      int var1;
      float var2;
      char var3;
   };
};

int main(){

   struct mystruct data;

   data.var = 10; 
   data.var2 = 5.55;

   printf("mystruct.var: %d\n", data.var);
   printf("anonymous union elements: %d %f %c", data.var1, data.var2, data.var3);

   return 0;
}

Output

Run the code and check its output −

mystruct.var: 10
anonymous union elements: 1085381018 5.550000 �

Note: Like a regular union, the uninitialized members of an anonymous union variable also show garbage value.

Advantages of Anonymous Struct and Union

One of the main advantages is the ability to access the members directly without any inner struct or union name. This can make the code more readable. Here is a list of some other advantages of using anonymous structures and unions −

Memory Efficiency − Like regular unions, anonymous unions allow different data types to share the same memory space, leading to more memory-efficient code, especially useful in low-memory environments.

Flexibility − Anonymous structures provide flexibility in how data is represented and accessed, allowing for more dynamic and versatile data structures.

Convenience − This feature allows for a compact representation of a variable that can hold different data types.

Ease of Initialization − They can be easier to initialize and use, as they do not require the declaration of a union variable.

Note that anonymous struct or union types were not part of the C standard before C11. Hence, their use in the code may lead to compatibility problems, if the target system uses a compiler compliant with earlier standards.

Advertisements