473,795 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

define a struct that has its own type as a field somehow?

Ben
Hello,

Is it possible to define a struct that has a type of itself in it?

So is it possible and if so how to do what I'm trying to do here?:

typedef struct mystruct {
int a;
mystruct *b;
} mystruct;

The above doesn't work:

test.c:7: undefined type, found `mystruct'
cpp-precomp: warning: errors during smart preprocessing, retrying in
basic mode
test.c: In function `main':
test.c:7: parse error before "mystruct"
test.c:7: warning: no semicolon at end of struct or union
test.c: At top level:
test.c:8: warning: data definition has no type or storage class
test.c:10: parse error before "return"

Thanks, Ben.
Oct 26 '06 #1
10 1508

"Ben" <x@x.abcwrote in message news:26******** **********@x.ab c...
Hello,

Is it possible to define a struct that has a type of itself in it?
Thanks, Ben.
Try this
typedef struct _mystruct {
int a;
struct _mystruct *b;
} mystruct;

Serafeim
Oct 26 '06 #2
Ben
In article <26************ ******@x.abc>, Ben <x@x.abcwrote :
Hello,

Is it possible to define a struct that has a type of itself in it?

So is it possible and if so how to do what I'm trying to do here?:

typedef struct mystruct {
int a;
mystruct *b;
} mystruct;

The above doesn't work:

test.c:7: undefined type, found `mystruct'
cpp-precomp: warning: errors during smart preprocessing, retrying in
basic mode
test.c: In function `main':
test.c:7: parse error before "mystruct"
test.c:7: warning: no semicolon at end of struct or union
test.c: At top level:
test.c:8: warning: data definition has no type or storage class
test.c:10: parse error before "return"

Thanks, Ben.
Oops, I've just found a section in the K&R book titled
"Self-referential Structures" ! :) I think that will answer the
question... and it does. The answer is:

struct mystruct {
int a;
struct mystruct *b;
};
Oct 26 '06 #3
Ben
In article <eh***********@ ulysses.noc.ntu a.gr>, Papastefanos Serafeim
<se******@otene t.grwrote:
"Ben" <x@x.abcwrote in message news:26******** **********@x.ab c...
Hello,

Is it possible to define a struct that has a type of itself in it?
Thanks, Ben.

Try this
typedef struct _mystruct {
int a;
struct _mystruct *b;
} mystruct;

Serafeim
Yup, thanks. 'struct' infront does it.
Thanks, Ben.
Oct 26 '06 #4
>"Ben" <x@x.abcwrote in message news:26******** **********@x.ab c...
>Is it possible to define a struct that has a type of itself in it?
Thanks, Ben.
See <http://web.torek.net/torek/c/types2.html>.

In article <eh***********@ ulysses.noc.ntu a.gr>
Papastefanos Serafeim <se******@otene t.grwrote:
>typedef struct _mystruct {
int a;
struct _mystruct *b;
} mystruct;
Avoid names that start with a leading underscore. (Most of those
names are reserved to me, the implementor, rather than you, the
user. I have to avoid names that do *not* start with underscores,
because those names are reserved to you, the user.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 26 '06 #5
On Thu, 26 Oct 2006 13:51:05 GMT, in comp.lang.c , Ben <x@x.abc>
wrote:
>Hello,

Is it possible to define a struct that has a type of itself in it?

So is it possible and if so how to do what I'm trying to do here?:

typedef struct mystruct {
int a;
mystruct *b;
} mystruct;

The above doesn't work:
you need a tentative definition of the struct before you use it:

struct mystruct;

struct mystruct
{
int a;
struct mystruct* b;
};
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 26 '06 #6
Mark McIntyre <ma**********@s pamcop.netwrite s:
On Thu, 26 Oct 2006 13:51:05 GMT, in comp.lang.c , Ben <x@x.abc>
wrote:
>>Is it possible to define a struct that has a type of itself in it?

So is it possible and if so how to do what I'm trying to do here?:

typedef struct mystruct {
int a;
mystruct *b;
} mystruct;

The above doesn't work:

you need a tentative definition of the struct before you use it:

struct mystruct;

struct mystruct
{
int a;
struct mystruct* b;
};
No, you don't; all you need is:

struct mystruct {
int a;
struct mystruct *b;
};

The name of a typedef doesn't become visible until the end of the
declaration. A struct tag becomes visible as soon as it's mentioned.
(It's an incomplete type until the end of the declaration, but that's
ok because we're just declaring a pointer to it.)

This is a good argument for *not* using typedefs for structures.
But if you insist on using a typedef (i.e., having a duplicate name
"mystruct" for something that already has a perfectly good name
"struct mystruct"), you can do this:

typedef struct mystruct {
int a;
struct mystruct *b;
} mystruct;

or this:

typedef struct mystruct mystruct;
struct mystruct {
int a;
mystruct *b;
};

But I prefer to declare it like this:

struct mystruct {
int a;
struct mystruct *b;
};

and just refer to the type as "struct mystruct". (Using a typedef
makes sense if I want it to be an abstract type, i.e., if I want to
hide the fact that it's a struct.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 26 '06 #7
Ben <x@x.abcwrote :
# Hello,
#
# Is it possible to define a struct that has a type of itself in it?
#
# So is it possible and if so how to do what I'm trying to do here?:
#
# typedef struct mystruct {
# int a;
# mystruct *b;
# } mystruct;

typedef struct mystruct mystruct;
struct mystruct {
int a;
mystruct *b;
};

--
SM Ryan http://www.rawbw.com/~wyrmwif/
But I do believe in this.
Oct 27 '06 #8
On Thu, 26 Oct 2006 21:02:36 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.orgw rote:
>Mark McIntyre <ma**********@s pamcop.netwrite s:
>On Thu, 26 Oct 2006 13:51:05 GMT, in comp.lang.c , Ben <x@x.abc>
wrote:
>>>typedef struct mystruct {
int a;
mystruct *b;
} mystruct;

The above doesn't work:

you need a tentative definition of the struct before you use it:
No, you don't;
The name of a typedef doesn't become visible until the end of the
declaration. A struct tag becomes visible as soon as it's mentioned.
Interesting - I've always erred on the side of caution.
>This is a good argument for *not* using typedefs for structures.
I'm with you on this, not a big fan of hiding types.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 27 '06 #9
Mark McIntyre <ma**********@s pamcop.netwrote :

# >This is a good argument for *not* using typedefs for structures.
#
# I'm with you on this, not a big fan of hiding types.

The joke being that struct tags were a hack to allow a
one-pass compiler with Algol style declarations instead
of Pascal declarations (Pascal declarations don't have
one-pass parsing problems). Now the hack is enshrined
as good programming practice.

Anytime you can get your hacks and patches to be accepted
as features, that's good hackery.

Today there's no good reason not to save the symbol table
and let the edittor be able to show you the symbol definition
instead of sophomoric tricks like splitting a function type
and function name on two separate lines so you can find
function defintions with grep.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If your job was as meaningless as theirs, wouldn't you go crazy too?
Oct 28 '06 #10

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

Similar topics

97
27822
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
2
4520
by: psundara | last post by:
Hi, I'm facing a peculiar problem of finding a way to interpret header information in a smart way. I have this header file that is shared by many users, which contains, among things, a few #define statements that associate GUIDs wih a friendly name. For e.g. consider the file zoo.h: ....
4
11112
by: Phui Hock | last post by:
Hi, I'm a newbie in C. Can anyone tell me why the size of 'test' is 8 bytes instead of 4 bytes when I uncomment the 'struct list *next;'? I hope someone can can explain to me. I can't find answer anywhere in my references nor on the net. Thank you very much. #include <stdint.h> typedef struct list{ uint8_t a :6;
3
10585
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
42
5634
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
12
3607
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type, reads it field by field, encodes
10
2607
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory to be used as a Type structure. The background: I was beaten many times (it surely is not my exclusive experience) by functions which return and accept void*. There are such functions,
4
2213
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
7
1368
by: lyf2iis | last post by:
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
0
9672
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, 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...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10436
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, 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...
0
10213
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 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...
0
10000
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 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...
0
9040
agi2029
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...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
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
3
2920
bsmnconsultancy
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...

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.