RE: Linked lists in C and C++.

  • From: "Graham Hardy" <graham.hardy@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 12 Oct 2007 19:28:29 -0700

Hi Laura et al. - Thanks for these explanations; now I can convert the code
that I wrote to remove the hideous void pointer casting/dereferencing mess
(which does work, by the way). I've also just realised that there are
classes such as 'vector' which might be able to do the same thing, but I
figured my way would not incur much loss of performance, or should I not be
worrying about that? And Sina - no, I'm not the type to forget the fact that
the link should be a pointer and create a massive runtime error, although I
must admit it would be a lot of fun to try!


-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Laura Eaves
Sent: October 10, 2007 4:55 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Linked lists in C and C++.

Hi Graham --
First, you should put a name after the struct keyword, say like the

typedef struct token_t { struct token_t *next; } token_t;

That should work in both C and C++ -- Note that here is a major departure of

C++ syntax from C: In C, the struct tag names are in their own symbol 
C++ table
so you can say things like

struct stat stat;

where the object has the same name as the struct tag.
But in C++, struct tags are in the same scope as variable and function and
type names, so the "struct stat stat;" declaration is an error.
Now you might ask why C++ would allow the above typedef of token_t since
that name clashes with the struct tag.  Well the answer is that C++ makes
this exception so that its code will compile C code a little easier.

But back to your example, you can put in the struct tag or omit it but in
the declaration of next, you must use the struct keyword, even in C++ as
token_t has not been defined.

Now as for pragmas, these are implementation dependent I believe.  There is
a special character type called wchar_t that accomodates wide characters,
otherwise a char is one byte.
But if you want to be sure a long int variable for example is packed with
chars, you can always use sizeof(long) to get the actual number of bytes in
a long int and proceed from there.

----- Original Message -----
From: "Graham Hardy" <graham.hardy@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, October 10, 2007 7:37 PM
Subject: Linked lists in C and C++.

Hi all,

I am creating a linked list in a C++ program, which I can envision as

typedef struct
 /* ... */
 token_t *next;

(Yes, this list only needs to be linked in one direction.) Clearly the
problem is that when I declare the pointer to the next object, though it is
a pointer of the type of the members in my list, since that type has not
been declared yet, I don't think I can do that. My first instinct told me to
use void pointers. Alternatively, I suppose I could declare the struct
giving the typename after the keyword 'struct' and before the '{', as,
struct token_t { /* ... */ }; Does this method change anything in the way
that types are concerned? That is, what is the diference between struct A
{/*...*/}; and typedef struct {/*...*/} B;?

And one more question. Despite the 'size' of a given data type being
explicit (as, say, a char being 1 byte), I am led to believe that where the
actual heap is concerned the alignment is not quite the same, such as 4 or 8
bytes for a char. I believe one can use the directives such as--

#pragma pack (...)

or something akin to that. If so, how does the syntax of this pack function
work? It seems that it begins with a keyword of some sort, as to whether the
previous alignment should be retained for the future, and then a new value.
Could someone enlighten me?

Thanks in advance,


View the list's information and change your settings at

View the list's information and change your settings at 

View the list's information and change your settings at 

Other related posts: