Topic: typedef, pointers, linked lists
Author: blargg@flash.net (Gargantua Blargg)
Date: 1999/06/13 Raw View
In article <7jnfeu$805$1@nnrp1.deja.com>, wkmon <wkmon@my-deja.com> wrote:
...
> struct ListItem;
>
> typedef ListItem* ListItemPtr;
>
> struct ListItem
> {
> int data;
> ListItemPtr link;
> };
...
I'm at a loss as to why this isn't just written as
struct ListItem
{
int data;
ListItem* link;
};
I don't see what is gained with the typedef. ListItem* seems perfectly
clear, as does ListItem const* and ListItem&. I guess one could add
typedefs for these... ListItemPtr, ConstListItemPtr (or is it
ListItemConstPtr?), ListItemRef, oh and ConstListItemRef, and
StdVectorListItemIterator... you get the idea. These kinds of typedefs
seem useless, as they don't encapsulate anything.
Stop the clutter :-)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/11 Raw View
In article <37600BB5.71C66512@hotmail.com> Peter Bohne <pbohne@hotmail.com> writes:
>Here's yet another way to do it:
>
>typedef struct ListItem {
> int theData;
> struct ListItem *next;
>} LIST_ITEM;
>
>then, in your working code, you can say things like:
>
>LIST_ITEM theListHead;
>/* do stuff, add more dynamically allocated nodes, etc. */
>
>It's all standard C++ (& C, as noted by a previous poster), and it goes to
>show you that there's lots of different ways to do the same kind of thing.
Right, except as I recall the original issue, the poster wanted to get
a "pointer type" in the end too:
typedef struct LI { ... } *pLI;
pLI p = &theListHead....
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/09 Raw View
In article <7jkkun$7fj$1@nnrp1.deja.com> wkmon <wkmon@my-deja.com> writes:
>struct ListItem;
This is an `elaborated-type-specifier'. It effectively serves as
a so-called forward declaration.
Do note though that ListItem is an incomplete type at that point.
So although you could do this:
ListItem *p;
You could not do this:
ListItem li;
and so on.
>I am wondering if this (or any of the above code) is standard C++,
Yes, It's Standard C++ _and_ Standard C.
>or if anyone has any suggestions, advice, words of wisdom? It looks a
>bit strange to me,
I guess you need to get used to it. It's really the only way to
do self-referential kinda stuff, and other things.
>and my instructor had not seen it before.
>I have not been able to find any info on this anywhere so far.
Although in the grand scheme of things it's just a syntactic nuance,
I'm surprised that your instructor would not know about it.
Anybody who has written a reasonable amount of code should know this.
Probably (w/o knowing which you have) you should consider additional
texts too. In general, that's a good idea.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wkmon <wkmon@my-deja.com>
Date: 1999/06/10 Raw View
Sorry about the typos (first post jitters, no doubt).
Should read:
struct ListItem;
typedef ListItem* ListItemPtr;
struct ListItem
{
int data;
ListItemPtr link;
};
Thanks for the responses,
Warren
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Peter Bohne <pbohne@hotmail.com>
Date: 1999/06/10 Raw View
Here's yet another way to do it:
typedef struct ListItem {
int theData;
struct ListItem *next;
} LIST_ITEM;
then, in your working code, you can say things like:
LIST_ITEM theListHead;
/* do stuff, add more dynamically allocated nodes, etc. */
It's all standard C++ (& C, as noted by a previous poster), and it goes to
show you that there's lots of different ways to do the same kind of thing.
pete
---------------- pbohne at hotmail dot com -----------------
Peter Bohne HBO & Co Louisville, CO 303-926-2218
"Very funny, Scottie. Now beam down my clothes!"
------------ (also at:) pbohne at hboc dot com -----------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wkmon <wkmon@my-deja.com>
Date: 1999/06/09 Raw View
I am currently taking a class in C++. We are encouraged to typedef
pointers to the various data types in our programs. While working with
a linked list program I came across the dilemma of not being able to
typedef a pointer to a struct that had not yet been defined. This is
basically what I ended up doing:
struct ListItem
{
int data;
ListItem* link;
};
typedef ListItem* ListItemPtr;
This worked fine; I could use ListItemPtr to declare pointers to a
ListItem throughout the rest of the program.
By accident, during a cut/paste session trying some different things, I
found the following code compiled and worked without any warnings or
errors.
struct ListItem;
typedef ListItem* ListItemPtr;
struct LIstItem
{
int data;
ListItemPtr;
};
I am wondering if this (or any of the above code) is standard C++,
or if anyone has any suggestions, advice, words of wisdom? It looks a
bit strange to me, and my instructor had not seen it before.
I have not been able to find any info on this anywhere so far.
Thanks,
Warren
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 1999/06/09 Raw View
In article <7jkkun$7fj$1@nnrp1.deja.com>,
wkmon <wkmon@my-deja.com> wrote:
>
> By accident, during a cut/paste session trying some different things,
I
> found the following code compiled and worked without any warnings or
> errors.
>
> struct ListItem;
>
> typedef ListItem* ListItemPtr;
>
> struct LIstItem
> {
> int data;
> ListItemPtr;
> };
>
> I am wondering if this (or any of the above code) is standard C++,
> or if anyone has any suggestions, advice, words of wisdom? It looks a
> bit strange to me, and my instructor had not seen it before.
> I have not been able to find any info on this anywhere so far.
Except for typos, this is perfectly fine standard C++. The construct
"class ListItem" is called an elaborated-type-specifier and is
described in the standard in sections 3.4.4 and 7.1.5.3. Generally,
if the name in the elaborated-type-specifier has not been declared
yet, use of an elaborated-type-specifier in a declaration declares
the name as an incomplete type (if it's a class declaration; you can
use an elaborated-type-specifier for an enum, but the enum must have
been already declared). You can make and use pointers to incomplete
types, but you can't dereference such a pointer or use the incomplete
type in a context where its size or members are needed until the
complete definition is seen.
You also can combine the declaration as an incomplete type and the
typedef of the pointer:
typedef struct ListItem* ListItemPtr;
This is exactly equivalent to the first two lines of your example
(unless ListItem had been declared in a containing scope; in that
case, you must use your form to declare that the typedef is a pointer
to the inner ListItem rather than to the ListItem in the containing
scope).
--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]