Anonymus Structs

Reply

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Anonymus Structs

 
0
  #1
Jun 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Anonymus Structs

 
-1
  #2
Jun 15th, 2009
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.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Anonymus Structs

 
0
  #3
Jun 16th, 2009
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).
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Anonymus Structs

 
0
  #4
Jun 16th, 2009
>>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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Anonymus Structs

 
0
  #5
Jun 16th, 2009
I think you are right.
Last edited by s_sridhar; Jun 16th, 2009 at 1:44 am.
Intel inside, mental outside
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Anonymus Structs

 
0
  #6
Jun 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Anonymus Structs

 
0
  #7
Jun 16th, 2009
>>The most natural place for structures without tags is typedef declaration

I forgot about those
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC