Don't understand this struct

Started by
6 comments, last by JoeZ 20 years, 9 months ago
struct _Node{ Node *next; Node *child; Node *parent; }; The structure name has an _ , but in the program the code still works even a variable is declared like Node node; why why why? Thanks
Advertisement
because that's not what you saw. (bad you! no cookie!) what you saw is this:

typedef struct _Node{
Node *next;
Node *child;
Node *parent;
} Node;

which typedefs "struct _Node" to "Node". It's a C thing.

EDIT: oh, yeah, people sometimes do it with #defines, too.
How appropriate. You fight like a cow.

[edited by - sneftel on July 2, 2003 12:19:45 PM]
Why do people do that?
Because you don't have to write (C only, C++ does not require you to write struct anyways, afaik)

struct _Node node;

but only

Node node;

I guess writing

typedef struct
{
Node *next;
Node *child;
Node *parent;
}
Node;

Might've been more obvious, without adding both Node and struct _Node (One is redundant, no?). *shrugs*

edit: typos

[edited by - Wildfire on July 2, 2003 12:25:37 PM]
How do I set my laser printer on stun?
// This is Cstruct Node {Node *next;Node *child;Node *parent;};// Now to create an instance...struct Node node;  // What the hell is that extra baggage?


...And, of course, that would have to be repeated every time you need to reference the type (like in a function declaration).

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Thanks for your reply

the actual code is there

00135 struct _Lib3dsNode {
00136 Lib3dsUserData user;
00137 Lib3dsNode *next;\
00138 Lib3dsNode *childs;\
00139 Lib3dsNode *parent;\
00140 Lib3dsNodeTypes type;\
00141 Lib3dsWord node_id;\
00142 char name[64];\
00143 Lib3dsWord flags1;\
00144 Lib3dsWord flags2;\
00145 Lib3dsWord parent_id;
00146 Lib3dsMatrix matrix;
00147 Lib3dsNodeData data;
00148 };
00149
00150 extern LIB3DSAPI Lib3dsNode* lib3ds_node_new_ambient();

see what I mean?
the _Lib3dsNode (line 135)
and the line 150 function returns Lib3dsNode * they look different

look for a #define Lib3dsNode struct _Lib3dsNode

[edited by - daerid on July 2, 2003 1:28:35 PM]
daerid@gmail.com
oh

That is why it is like that

Thanks to All

This topic is closed to new replies.

Advertisement