473,397 Members | 2,084 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,397 software developers and data experts.

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 1475

"Ben" <x@x.abcwrote in message news:26******************@x.abc...
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.ntua.gr>, Papastefanos Serafeim
<se******@otenet.grwrote:
"Ben" <x@x.abcwrote in message news:26******************@x.abc...
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.abc...
>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.ntua.gr>
Papastefanos Serafeim <se******@otenet.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**********@spamcop.netwrites:
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_Keith) 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.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>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**********@spamcop.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
Keith Thompson <ks***@mib.orgwrote:
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.
I don't know if I'd say it's a good argument for not using typedefs
for structs - it seems to me to be mostly a matter of style religion
once one looks past the fact that the first time a new C programmer
attempts to do it, (s)he will almost invariably get it wrong.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 28 '06 #11

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
2
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...
4
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...
3
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
42
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
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,...
10
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...
4
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.