954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Anonymus Structs

Hello.,
I'm reading about anonymus structs and have problems understanding what is the privilege to have one. could anyone demonstrate an example of both anonymus and regular struct. I see that anonymus struct has a name after the the } and before semicolon. Any help would be greatly appreciated.

Thank you

atman
Junior Poster in Training
50 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

take a look at this

Anonymous structures are not standard C therefore they are implemented as extensions. Anonymous structures are only useful as pointer targets, i.e.
"struct A *foo". You can pass around pointers to structures without
knowing the size of the structure, although you can't dereference them.


Anonymous unions are standard C and they are can be found inside an object or inside a namespace.

1. When an anonymous union is declared inside of an object the role is to have all data members of that class or struct to share the same memory space and they are accessed as any other datamembers. This is not considered a healthy practice and it can be used only with the system that do not have a good support for typecasting. reinterpret_cast is the recommended way over anonymous unions.

2. An anonymous union can be declared inside a namespace or at the global scope but because the unions members require internal linkage the union must be declared static.

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

An anonymous struct can exist, but since you can't refer to it by a name, you need to instantiate it right away (unlike an anonymous union).

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

>>reinterpret_cast is the recommended way over anonymous unions.


reinterpret_cast is not allowed in C. So I don't know where you got your information but its wrong.


The only place I've used anonymous structs is inside a union

union something
{
    struct
    {
          int x;
          int y;
    };
    unsigned char buf[2 * sizeof(int)];
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I think you are right.

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

The most natural place for structures without tags is typedef declaration (it's the only place I've used anonymous structs ;)):

typedef struct 
{
  ...
} Record;
...
Record  rec;
Record* prec = &rec;
...

Of course, it's impossible to define self-referential structures in a such manner.

Yet another program case (less interesting):

struct { int x, y; } s;
struct { int x, y; } t;

Types of s and t are different. It's impossible to declare other variables of these types or to pass s and t as parameters.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

>>The most natural place for structures without tags is typedef declaration

I forgot about those :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You