the struct haven't define when i use typedef

L

lyf2iis

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.
thank you.
 
R

Richard Bos

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.

It doesn't have to be, because (C99, 6.2.5#27):

# All pointers to structure types shall have the same representation and
# alignment requirements as each other.

(And ditto for unions - but struct pointers and union pointers need not
be the same as one another.)

So the implementation doesn't have to know what kind of struct a struct
node is; all it needs to know is that it _is_ a struct. From that alone,
it can decide what kind of pointer to use.

Richard
 
C

Chad

(e-mail address removed) said:





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;

};

Maybe this is a bit off topic, but how does the compiler know how big

typedef struct node_ node;

if this comes before

struct node_
{
char *item;
node *next;

};
 
J

jameskuyper

Chad said:
Maybe this is a bit off topic, but how does the compiler know how big

typedef struct node_ node;

if this comes before

struct node_
{
char *item;
node *next;

};

It doesn't. The compiler doesn't need to know how big "node" is. All
it needs to know is how big "node*" is.
 
C

Chad

It doesn't. The compiler doesn't need to know how big "node" is. All
it needs to know is how big "node*" is.

Is this determined at compile time? If so, how does the compiler know
how much space to allocate for node*?
 
J

jameskuyper

Chad wrote:
....
Is this determined at compile time? If so, how does the compiler know
how much space to allocate for node*?

Because all struct pointers have the same size, chosen by the
compiler, regardless of what struct type they point at.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top