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

B

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.
 
P

Papastefanos Serafeim

Ben said:
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
 
B

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.

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

Ben

Papastefanos Serafeim said:
Try this
typedef struct _mystruct {
int a;
struct _mystruct *b;
} mystruct;

Serafeim

Yup, thanks. 'struct' infront does it.
Thanks, Ben.
 
C

Chris Torek

See <http://web.torek.net/torek/c/types2.html>.

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.)
 
M

Mark McIntyre

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
 
K

Keith Thompson

Mark McIntyre said:
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.)
 
S

SM Ryan

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

Mark McIntyre

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
 
S

SM Ryan

# >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.
 
C

Christopher Benson-Manica

Keith Thompson said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top