Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 29th, 2008, 06:45 AM
lyf2iis@gmail.com
Guest
 
Posts: n/a
Default the struct haven't define when i use typedef

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.
  #2  
Old August 29th, 2008, 07:55 AM
Richard Bos
Guest
 
Posts: n/a
Default 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
  #3  
Old August 29th, 2008, 08:05 AM
Richard Heathfield
Guest
 
Posts: n/a
Default 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
  #4  
Old August 29th, 2008, 03:15 PM
Chad
Guest
 
Posts: n/a
Default 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;

};


  #5  
Old August 29th, 2008, 03:35 PM
jameskuyper@verizon.net
Guest
 
Posts: n/a
Default 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.
  #6  
Old August 29th, 2008, 03:55 PM
Chad
Guest
 
Posts: n/a
Default 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*?
  #7  
Old August 29th, 2008, 04:13 PM
Richard Heathfield
Guest
 
Posts: n/a
Default 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
  #8  
Old August 29th, 2008, 04:25 PM
jameskuyper@verizon.net
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles