Connecting Tech Pros Worldwide Help | Site Map

the struct haven't define when i use typedef

lyf2iis@gmail.com
Guest
 
Posts: n/a
#1: Aug 29 '08
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.
Richard Bos
Guest
 
Posts: n/a
#2: Aug 29 '08

re: the struct haven't define when i use typedef


lyf2iis@gmail.com wrote:
Quote:
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
Richard Heathfield
Guest
 
Posts: n/a
#3: Aug 29 '08

re: the struct haven't define when i use typedef


lyf2iis@gmail.com said:
Quote:
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
Chad
Guest
 
Posts: n/a
#4: Aug 29 '08

re: the struct haven't define when i use typedef


On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
Quote:
lyf2...@gmail.com said:
>
Quote:
hi, i write a code like this:
>
Quote:
typedef struct node *NODEPTR;
>
Quote:
struct node {
char *item;
NODEPTR next;
};
>
Quote:
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;
>
};
>
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;

};


jameskuyper@verizon.net
Guest
 
Posts: n/a
#5: Aug 29 '08

re: the struct haven't define when i use typedef


Chad wrote:
Quote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
....
Quote:
Quote:
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;
>
};
It doesn't. The compiler doesn't need to know how big "node" is. All
it needs to know is how big "node*" is.
Chad
Guest
 
Posts: n/a
#6: Aug 29 '08

re: the struct haven't define when i use typedef


On Aug 29, 7:31*am, jameskuy...@verizon.net wrote:
Quote:
Chad wrote:
Quote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
...
Quote:
Quote:
But on a style point, I recommend not hiding pointers behind typedefs.. I
would write the code like this:
>
Quote:
Quote:
typedef struct node_ node;
>
Quote:
Quote:
struct node_
{
* char *item;
* node *next;
>
Quote:
Quote:
};
>
Quote:
Maybe this is a bit off topic, but how does the compiler know how big
>
Quote:
typedef struct node_ node;
>
Quote:
if this comes before
>
Quote:
struct node_
{
* char *item;
* node *next;
>
Quote:
};
>
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*?
Richard Heathfield
Guest
 
Posts: n/a
#7: Aug 29 '08

re: the struct haven't define when i use typedef


Chad said:
Quote:
On Aug 29, 7:31 am, jameskuy...@verizon.net wrote:
<snip>
Quote:
Quote:
>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?
Yes.
Quote:
If so, how does the compiler know
how much space to allocate for node*?
Because node is a struct type (which the forward declaration tells the
compiler), node * is a struct pointer type - and the compiler certainly
knows how big its pointers-to-structure are.

--
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
jameskuyper@verizon.net
Guest
 
Posts: n/a
#8: Aug 29 '08

re: the struct haven't define when i use typedef


Chad wrote:
....
Quote:
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.
Closed Thread