473,811 Members | 3,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8296
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**********@i nvalid.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.ne t> 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_Keit h) 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**********@i nvalid.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**********@i nvalid.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**********@i nvalid.invalid> wrote:
Richard Bos schrieb:
Michael Mair <Mi**********@i nvalid.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
31580
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, a Class can have private and protected members, but a Struct everything is Public by default. I laymans terms what would be an appropriate reason to choose a Struct over a Class? So why would one want to choose a Class over a Struct.
10
1873
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 mechanism(s). The text file looks something like this: Name: David Age: 108 Birthday: 061095 SocialSecurity: 476892771999
6
4906
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
9
2258
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
9476
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 timespec.c:5: error: storage size of 'b' isn't known .... with:
4
5071
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
3
3892
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
2821
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 class. I need to store
16
2390
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
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.