473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to make a struct contain a ptr to itself?

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
7 8200
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
10
by: Ernesto | last post by:
I'm still fairly new to python, so I need some guidance here... I have a text file with lots of data. I only need some of the data. I want to put the useful data into an struct-like...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
9
by: Erik | last post by:
Hi, i have this struct and this linked list /* structure describing a book.*/ typedef struct { char code; char author; char title; int year; int reserved; } Book;
10
by: pocmatos | last post by:
Hi all, Using -std=c99 to enforce the c99 standard in gcc I get: $ gcc -Wall -std=c99 timespec.c timespec.c: In function 'main': timespec.c:5: error: storage size of 'a' isn't known...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
3
by: Benton | last post by:
Hi there, Currently I have a rather simple class with a few members: string userName; string password; string centers; bool canModify; I have correspondant GET properties for them in the...
16
by: =?iso-8859-1?Q?Daniel_Lidstr=F6m?= | last post by:
Hello! I have these following classes: public struct Move { public int From { get; set; } public int To { get; set; }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.