How to make a struct contain a ptr to itself?

S

Steve Edwards

Hi,


How do I create a new type of structure that contains a pointer to its
own type? i.e.

typedef struct{
string tag;
TagPtr *subPtr;
} TagPtr;

(...which won't compile as it doesn't know what TagPtr is for subPtr.)

I know how to do this with a C++ class by just declaring:
class TagPtr;
prior to the definition. Is there a similar syntax for a C struct?

Thanks

Steve
 
M

Michael Mair

Steve said:
Hi,


How do I create a new type of structure that contains a pointer to its
own type? i.e.

typedef struct{
string tag;
TagPtr *subPtr;
} TagPtr;

(...which won't compile as it doesn't know what TagPtr is for subPtr.)

I know how to do this with a C++ class by just declaring:
class TagPtr;
prior to the definition. Is there a similar syntax for a C struct?

Basically, you want

struct TagPtr_ {
string tag;
struct TagPtr_ *subPtr;
};
typedef struct TagPtr_ TagPtr;

Alternatives are
typedef struct TagPtr_ {
string tag;
struct TagPtr_ *subPtr;
} TagPtr;
and
typedef struct TagPtr_ TagPtr;
struct TagPtr_ {
string tag;
TagPtr *subPtr;
};
Note that instead of TagPtr_, you could also use TagPtr
as struct, union, and enum tags are in another namespace
than the typename identifier created by the typedef.

Cheers
Michael
 
R

ranjmis

Hi,

First you need to give a name for struct - say "tag_struct"
then there are two ways to achieve the goal one -
If u want to use new type field inside struct then u need to declare it
first.

----------
typedef struct tag_struct *TagPtr;

typedef struct tag_struct {
string tag; /* assuming 'typedef string char*;' exists */
TagPtr subPtr;
};
-----------
otherwise -
-----------
typedef struct tag_struct {
string tag;
struct tag_struct *subPtr;
} *TagPtr;

----------

Hope the info is of some help

Thanks
Ranjeet
 
S

Steve Edwards

Michael Mair said:
Basically, you want

struct TagPtr_ {
string tag;
struct TagPtr_ *subPtr;
};
typedef struct TagPtr_ TagPtr;

Alternatives are
typedef struct TagPtr_ {
string tag;
struct TagPtr_ *subPtr;
} TagPtr;
and
typedef struct TagPtr_ TagPtr;
struct TagPtr_ {
string tag;
TagPtr *subPtr;
};
Note that instead of TagPtr_, you could also use TagPtr
as struct, union, and enum tags are in another namespace
than the typename identifier created by the typedef.

Cheers
Michael



Thanks Sanjeet and Michael, all work well for my needs... I'm spoilt for
choice!
Cheers

Steve
 
K

Keith Thompson

Steve Edwards said:
How do I create a new type of structure that contains a pointer to its
own type? i.e.

typedef struct{
string tag;
TagPtr *subPtr;
} TagPtr;

(...which won't compile as it doesn't know what TagPtr is for subPtr.)

I presume you have a type called "string" somewhere. You really
shouldn't, since names starting with "str" are reserved, but we'll
leave that aside for the moment.

struct TagPtr {
string tag;
struct TagPtr *subPtr;
};

There's really no need for a typedef; just refer to the type by its
name, "struct TagPtr". <OT>C++ would let you call it "TagPtr"; C
doesn't.</OT>

If you insist on having a second name for the type, you can use a
typedef:

typedef struct TagPtr {
string tag;
struct TagPtr *subPtr;
} TagPtr;

This creates a structure type called "struct TagPtr", and an alias for
that type, "TagPtr". Struct tags and typedefs are in distinct
namespaces, so it's ok to use the same identifier for both. Now you
can refer to the type either as "struct TagPtr" or as "TagPtr". The
name "TagPtr" becomes visible only after the end of the declaration,
so within the declaration you can refer to it only as "struct TagPtr".

A lot of programmers do it this way, but if you drop the typedef, you
only have one name for the type, and you don't have to remember which
one you can use where.

Your terminology is confusing. The identifier following the keyword
"struct" is called a struct tag; additionally using the word "tag"
both as part of the type name and as a member name makes it hard to
keep track of everything. An identifier ending in "ptr" or "Ptr"
usually implies a pointer, but "struct TagPtr" is not a pointer type
(but subPtr is a pointer).

Finally, what is a "string"?
 
R

Richard Bos

Basically, you want

struct TagPtr_ {
string tag;
struct TagPtr_ *subPtr;
};
typedef struct TagPtr_ TagPtr;

In this, and all other examples, you can leave off the underscore.
struct TagPtr does not clash with typedef TagPtr. (The same is true for
unions.)

Richard
 
M

Michael Mair

Richard said:
In this, and all other examples, you can leave off the underscore.
struct TagPtr does not clash with typedef TagPtr. (The same is true for
unions.)

And how does this contradict the part you snipped?
"Note that instead of TagPtr_, you could also use TagPtr
as struct, union, and enum tags are in another namespace
than the typename identifier created by the typedef."


Cheers
Michael
 
R

Richard Bos

Michael Mair said:
And how does this contradict the part you snipped?
"Note that instead of TagPtr_, you could also use TagPtr
as struct, union, and enum tags are in another namespace
than the typename identifier created by the typedef."

It doesn't. I should ingest more caffeine. Sorry.

Richard
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top