473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.
Aug 29 '08 #1
7 1354
ly*****@gmail.com wrote:
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
Aug 29 '08 #2
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
Aug 29 '08 #3
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
lyf2...@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;

};
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;

};
Aug 29 '08 #4
Chad wrote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
....
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.
Aug 29 '08 #5
On Aug 29, 7:31*am, jameskuy...@verizon.net wrote:
Chad wrote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.invalidwrote:
...
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.
Is this determined at compile time? If so, how does the compiler know
how much space to allocate for node*?
Aug 29 '08 #6
Chad said:
On Aug 29, 7:31 am, jameskuy...@verizon.net wrote:
<snip>
>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.
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
Aug 29 '08 #7
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.
Aug 29 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
15
by: dutchgoldtony | last post by:
Hi all, I was just wondering if this is possible. I'm trying to implement a viterbi decoder in C and am creating an array of nodes (the struct), and an array of pointers to nodes (the member...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
8
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.