• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C structure question

Yohhan

Senior member
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?
 
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.
 
You don't have to uppercase the JUNK on the end (at least with any compiler I've ever used).
 
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;
 
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".
 
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? 😕 ) If so, what is the point of making them different?
 
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? 😕 ) If so, what is the point of making them different?

IIRC, generally you can get away with it, but it's non-standard C.
 
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.
 
Back
Top