- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Anonymous Structures and Unions in C
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 structure 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; }; };
Access Members of Anonymous Structure
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; }; };
Access Members of Anonymous Union
The members of the anonymous union can be accessed directly without using a union name.
Example
Anonymous unions do not have a name. The elements share the same memory location.
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.