ly*****@gmail.com said:
hi, i write a code like this:
typedef struct node *NODEPTR;
struct node {
char *item;
NODEPTR next;
};
my question is how the compiler can compile this code? the struct
haven't define when i use typedef.
At the time of your typedef, all the compiler really needs to know is how
much storage to reserve for objects of type NODEPTR. At this point, struct
node is an incomplete type (which you complete later): "If a type
specifier of the form
struct-or-union identifier
occurs prior to the declaration that defines the content, the
structure or union is an incomplete type", as the Standard puts it.
Since the compiler knows how big a pointer to a struct is, it has
everything it needs for now.
Of course, you can't point a NODEPTR at anything (if you don't count null
pointers) until the struct node type is completed.
But on a style point, I recommend not hiding pointers behind typedefs. I
would write the code like this:
typedef struct node_ node;
struct node_
{
char *item;
node *next;
};
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999