| | |
Anonymus Structs
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
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
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
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.
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
>>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
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
C Syntax (Toggle Plain Text)
union something { struct { int x; int y; }; unsigned char buf[2 * sizeof(int)]; }
Last edited by Ancient Dragon; Jun 16th, 2009 at 1:19 am.
The most natural place for structures without tags is typedef declaration (it's the only place I've used anonymous structs
):
Of course, it's impossible to define self-referential structures in a such manner.
Yet another program case (less interesting):
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.
): c Syntax (Toggle Plain Text)
typedef struct { ... } Record; ... Record rec; Record* prec = &rec; ...
Yet another program case (less interesting):
c Syntax (Toggle Plain Text)
struct { int x, y; } s; struct { int x, y; } t;
![]() |
Similar Threads
- Programming student find distances btwn cities using array of structs (C++)
- JSP iterator error with structs (JSP)
- working with array of structs (C++)
- GTK code uses pointer for structs and not normal instances why? (C)
- 2 structs (C++)
- structs vs classes? (C++)
- Question about putting structs into arrays (C++)
Other Threads in the C Forum
- Previous Thread: undefined reference to...
- Next Thread: ptr and dynamic mem...
| Thread Tools | Search this Thread |
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram homework i/o ide include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pdf pointer pointers posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






