By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
>"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 (4039.22'N, 11150.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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.