C structure question

Yohhan

Senior member
May 17, 2002
263
0
0
I need a C structure that can have a pointer to itself... ie:

typedef struct
{
junk* j;
} junk;

Is there a way to do this?
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Just to clarify, at the time you're declaring the struct, the typedef hasn't really "happened" yet, so the compiler doesn't know what a "junk" is. If you write it the way FishTaco wrote it, it knows there is something called "struct junk" (not sure yet what it is), but it can create a pointer to the struct junk.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You don't have to uppercase the JUNK on the end (at least with any compiler I've ever used).
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: kamper
You don't have to uppercase the JUNK on the end (at least with any compiler I've ever used).

Case doesn't really matter on any names you define yourself.

edit: And I usually do something like:

typedef struct _junk
{
struct _junk * j;
} junk;
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: BFG10K
Typedef is only for creating aliases. This should work fine:

struct Struct_Type
{
Struct_Type ptr;
};


Struct_Type there is only a struct, not a type; you need "struct Struct_Type".
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: BingBongWongFooey
Originally posted by: kamper
You don't have to uppercase the JUNK on the end (at least with any compiler I've ever used).

Case doesn't really matter on any names you define yourself.

edit: And I usually do something like:

typedef struct _junk
{
struct _junk * j;
} junk;

I was just trying to say that the name at the top and the name at the bottom can be the same (can't they? :confused: ) If so, what is the point of making them different?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: kamper
I was just trying to say that the name at the top and the name at the bottom can be the same (can't they? :confused: ) If so, what is the point of making them different?

IIRC, generally you can get away with it, but it's non-standard C.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Originally posted by: BFG10K
Struct_Type there is only a struct, not a type;
Struct_Type is exactly what it's called: a type that is also a structure.

No. Your struct doesn't even compile! struct Struct_Type is a type. Struct_Type is not, until you typedef it.