anonymous structures/unions in C with example
What are anonymous structures/unions?
Structures/unions with no names are called anonymous structures/unions. They are also called unnamed unions/structures.
As there are no names, we cannot create variables of anonymous structures/unions, but we can use them in nested structures or unions.
Real World Example of anonymous structures/unions:
A status register of a microcontroller can have multiple bits. Sometimes we read the value of each bit, and sometimes the whole register. We can use anonymous structure/unions in this to easily refer to both.
Code:
Output:
$ ./anonymous
Status register value:0x83
Structures/unions with no names are called anonymous structures/unions. They are also called unnamed unions/structures.
As there are no names, we cannot create variables of anonymous structures/unions, but we can use them in nested structures or unions.
Real World Example of anonymous structures/unions:
A status register of a microcontroller can have multiple bits. Sometimes we read the value of each bit, and sometimes the whole register. We can use anonymous structure/unions in this to easily refer to both.
Code:
Output:
$ ./anonymous
Status register value:0x83
Comments
Post a Comment