Anonymous Union/Structure (or) Unnamed Union/Structure

When the structure/union tag name is not present, the structure is called an anonymous structure/union



This program returns the below errors, and please ignore the error 'flexible array member in union'. These anonymous structure members can be accessed only within the scope of where it is defined. It means if the structure is defined within main(), members should be accessible.



From the above I understood, we will not be able to directly access the anonymous structure members. But, when I started using a structure with tag, I was able to access it. 



The anonymous structure members are accessible if the parent structure has a valid tag name.    



I don't think still it is not useful to use anonymous structure inside another(fruits) which has tag because I can directly have these members (apple & banana) under fruits instead of anonymous structure. 

Later, I was thinking why can't use these anonymous structure for duplicate members, but still it again threw an error ' error: duplicate member ‘apple’'



Then it occurred to me that If we tweak the union usage little, the purpose of these anonymous union will be known. At this point, I haven't got a clue about the usage of anonymous structure.  Click here to learn union and tagging. 





Based on premium channels subscribers subscribed to, either one of the premium channels will be provided along with all other ordinary parameters(default channels). Get the program here

So, the purpose of an anonymous union is only if you feel lazy to use structure name, it would be the use case. <wink>

Also, want to know about forward declaration ? Click here.

Comments