Archived

This topic is now archived and is closed to further replies.

JoeZ

Don't understand this struct

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

// This is C


struct 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
______________________________________________________________

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites