943,737 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 551
  • C RSS
Jun 15th, 2009
0

Anonymus Structs

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jun 15th, 2009
-1

Re: Anonymus Structs

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.
Last edited by s_sridhar; Jun 16th, 2009 at 12:01 am.
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
Jun 16th, 2009
0

Re: Anonymus Structs

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).
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
Jun 16th, 2009
0

Re: Anonymus Structs

>>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
  1. union something
  2. {
  3. struct
  4. {
  5. int x;
  6. int y;
  7. };
  8. unsigned char buf[2 * sizeof(int)];
  9. }
Last edited by Ancient Dragon; Jun 16th, 2009 at 1:19 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jun 16th, 2009
0

Re: Anonymus Structs

I think you are right.
Last edited by s_sridhar; Jun 16th, 2009 at 1:44 am.
Reputation Points: 17
Solved Threads: 16
Junior Poster
s_sridhar is offline Offline
139 posts
since Mar 2009
Jun 16th, 2009
0

Re: Anonymus Structs

The most natural place for structures without tags is typedef declaration (it's the only place I've used anonymous structs ):
  1. typedef struct
  2. {
  3. ...
  4. } Record;
  5. ...
  6. Record rec;
  7. Record* prec = &rec;
  8. ...
Of course, it's impossible to define self-referential structures in a such manner.

Yet another program case (less interesting):
  1. struct { int x, y; } s;
  2. 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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Jun 16th, 2009
0

Re: Anonymus Structs

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

I forgot about those
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: undefined reference to...
Next Thread in C Forum Timeline: ptr and dynamic mem...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC