typedef structure?
I've been looking for info about the "typedef struct" in C++, but none of the articles seem to explain it in a way to understand it. Is it the same as a regular struct? What differences does it have? (If looked all over Google and MSDN)
A long time ago, in a galaxy far, far away...
C compilers didn't automatically generate an identifier when structs were declared.
This means:
would throw a compile-time error, because Bob isn't registered as a valid identifer. There were two ways around this:
Please do not do this in your C++ code though! Like SiCrane said, modern compilers don't have this problem!
C compilers didn't automatically generate an identifier when structs were declared.
This means:
struct Bob { ... };Bob b;
would throw a compile-time error, because Bob isn't registered as a valid identifer. There were two ways around this:
// The crappy way (prefix every instanciation declaration with 'struct'):struct Bob b;// The better way (typedef the struct):typedef struct Bob_ { ... } Bob;Bob b; // this now works properly, thanks to the typedef
Please do not do this in your C++ code though! Like SiCrane said, modern compilers don't have this problem!
If you define a struct in C like this:
and you refer to that type, you always have to add the 'struct' keyword in front of it. If you typedef it like this:
then you can also refer to the type without having to type the 'struct' keyword.
In C++ you don't need this, since the first version already allows you to refer to the type by its name (without 'struct').
Edit:
Seems nilkn was faster.
struct MyStruct { ... };
and you refer to that type, you always have to add the 'struct' keyword in front of it. If you typedef it like this:
typedef struct MyStruct { ... } MyStruct;
then you can also refer to the type without having to type the 'struct' keyword.
In C++ you don't need this, since the first version already allows you to refer to the type by its name (without 'struct').
Edit:
Seems nilkn was faster.
Quote:Original post by nilkn
A long time ago, in a galaxy far, far away...
C compilers didn't automatically generate an identifier when structs were declared.
Please note that C compilers still doesn't generate type tags for structs. It's a difference between C and C++, not a problem that has been fixed in "modern compilers".
Sorry for hijacking this slightly but I have a question. I have been using structs by putting struct infrount of them every time which is hastle. So I tried the typedef way but if theres one in the actual structure it doesn't work. E.g. in a linked list of bob structures you would have a bob pointer for the next in the list inside the bob structure. How would I get around that?
You have to use the struct tag to declare pointers to the struct type inside the struct definition. e.g.:
edit: again, only do this if you are using C. In C++ should be avoided.
typedef struct tagNode { struct tagNode * next; struct tagNode * prev;} Node;
edit: again, only do this if you are using C. In C++ should be avoided.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement