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

How to make a struct contain a ptr to itself?

P: n/a
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
Mar 4 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Steve Edwards schrieb:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 4 '06 #2

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

Mar 4 '06 #3

P: n/a
In article <46************@individual.net>,
Michael Mair <Mi**********@invalid.invalid> wrote:
Steve Edwards schrieb:
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


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

Steve
Mar 4 '06 #4

P: n/a
Steve Edwards <gf*@lineone.net> writes:
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"?

--
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.
Mar 4 '06 #5

P: n/a
Michael Mair <Mi**********@invalid.invalid> wrote:
Steve Edwards schrieb:

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;
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
Mar 6 '06 #6

P: n/a
Richard Bos schrieb:
Michael Mair <Mi**********@invalid.invalid> wrote:
Steve Edwards schrieb:
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;

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


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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 6 '06 #7

P: n/a
Michael Mair <Mi**********@invalid.invalid> wrote:
Richard Bos schrieb:
Michael Mair <Mi**********@invalid.invalid> wrote:
Steve Edwards schrieb:

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;

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


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
Mar 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.