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. 7 1369 ly*****@gmail.c om 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 ly*****@gmail.c om 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
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
lyf2...@gmail.c om 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;
};
Chad wrote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
....
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.
On Aug 29, 7:31*am, jameskuy...@ver izon.net wrote:
Chad wrote:
On Aug 29, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
...
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*?
Chad said:
On Aug 29, 7:31 am, jameskuy...@ver izon.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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 struct foobar;
^
|
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;
} RtWidget;
|
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
{
|
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 I'm worried about)
connecting to it like so:
// snippet start
typedef struct _node{
char state; // The state of each node ("00","01","10","11")
|
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
| |
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 familiar with "typedef int NUMBER", but how does it work with
structures.
Tilak
|
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
|
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 pointers have to
point to the full node type, and have to be referenced before that
full node type is known.
One solution is to use the 'fixpoint construction' to get an apparent
circular dependency...
class c_Final : public c_Layer2< c_Layer1...
|
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 struct in the header file and
including this file in all source files that need it? For example:
mystruct.h
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |