Advertisement

I didn't know this after 14 years of C/C++

Started by April 15, 2000 06:46 PM
16 comments, last by milo2120 24 years, 6 months ago
I''ve been programming C/C++ for 14 years (I used it for a year before they taught a class at the university) and I just learned something I didn''t know. I''ve tried this in MS Visual C/C++ 6.0 and Borland C/C++ 5.0 with the same result (better error message in VC btw). If anyone having a pure C compiler tries this, could let me know what happens, please. This problem started while working on a template class for doubly linked lists (i''m entertaining myself), but its the class definition not the template that is the problem. Here is the problem using only classes: Originally I wrote something like this, class Node { Node* prev, next; } which gets gets a compile-time error. In VC it says something about ''next'' and the fact that ''Node'' is being defined. A slight change to the above - class Node { Node* prev; Node* next; } gets no compile-time error. What surprised me was that I thought these were equivalent. They are for if you replace ''Node*'' in the class body with ''int*'' and both versions compile or substitute an already defined structure for ''Node''. I guess there is some syntactical rule having to do with self referential data structures that makes the first version illegal, but I haven''t found any documentation to explain or note this fact. If anyone can turn me on to something about this let me know. Thanks, Mike Roberts aka milo mlbobs@telocity.com
I think that if you would declare them like this:
Node *next,*prev;

It should work

If you code it, they will come...

Commander M
http://commanderm.8m.com
cmndrm@commanderm.8m.com
Advertisement
small mistake i think.
class Node
{
Node* prev, next;
}

is eq to
class Node
{
Node* prev;
Node next;
}

Note that there is no * in front of next...
oops somebody replied already....





-kertropp

C:\Projects\rg_clue\ph_opt.c(185) : error C3142: 'PushAll' :bad idea
C:\Projects\rg_clue\ph_opt.c(207) : error C324: 'TryCnt': missing point
-kertropp C:Projectsrg_clueph_opt.c(185) : error C3142: 'PushAll' :bad ideaC:Projectsrg_clueph_opt.c(207) : error C324: 'TryCnt': missing point
Duh!
Dang!

Somehow I had convinced myself that my first C++ book (C++ for C Programmers by Ira Pohl) that I got in 1990 said there was a change in pointer declaration syntax (it doesn''t). I though the change resulted in the fact that the line ''int* p, q;'' created two integer pointers and that ''int *p, q'' would be one integer pointer and one integer.

I guess I got confused because I had always used the form ''int *p, *q'' and in Pohl''s book he uses two lines, ''int *p;'' and ''int *q''. For some reason this screwed with my mojo and I thought that ''int* p, q'' would do the job. )

Funny thing is that at work (pure C stuff) I still use ''int *p'' to stay in style with some older code (older code of mine) and at home I must have never needed to declare two pointers at home where I had adopted ''int* p;''. So when I needed two pointers I just did ''int* p, q'' which is dead wrong.

About two minutes after reading Kertropp''s post (thanks for the pointing out the obvious Kertropp! ) I found in my newer C++ book (2nd Edition C++ Primer Plus by Stephen Prata)on page 121 a very specific warning to not do what I did. Dang nabbit and sufferin succotash! And to think I was one those C programmers who, due to earlier assembly programming experience, pretty quickly (and fully) understood pointers.

The real irony I guess is that I bought Pohl''s book because I learned C from a book he wrote with Al Kelley called ''A Book on C'' that I loved so much, as was so complete, that I never needed another. I''ve never even needed the ''must have book'' by Kerningham and Ritchie. And then ole Ira screws with mind!

Michael L. Roberts
aka milo
mlbobs@telocity.com
I had a problem like this and never new why it worked when I changed it to two lines of code. Thanks for clearing that up
nothing to be ashamed of, this happened to me just one week ago.
(but i didn''t post it up the board.. )
Advertisement
man, that is annoying! I ran into the same problem a while ago when making my own linked list class.

Doesn''t it seem more logical that
Blah * prev, next;
*should* be the same as
blah * prev,
* next;

or whatever... I guess that''s why there is usually a data type, then an LP data type in most code.
ie
BYTE a, b;

just so you don''t have to put that damn asterisk! =)
LPBYTE a, b;
___________________________Freeware development:ruinedsoft.com
One thing I always make a point of doing is to attach the notation to the variable, not the type.

Rather than
String * pString;

I have
String *pString;

Just makes it a bit more obvious to me. Some books seem to do the opposite, putting the adornment on the type, which is downright confusing.

(my byline from the Gamedev Collection series, which I co-edited) John Hattan has been working steadily in the casual game-space since the TRS-80 days and professionally since 1990. After seeing his small-format games turned down for what turned out to be Tandy's last PC release, he took them independent, eventually releasing them as several discount game-packs through a couple of publishers. The packs are actually still available on store-shelves, although you'll need a keen eye to find them nowadays. He continues to work in the casual game-space as an independent developer, largely working on games in Flash for his website, The Code Zone (www.thecodezone.com). His current scheme is to distribute his games virally on various web-portals and widget platforms. In addition, John writes weekly product reviews and blogs (over ten years old) for www.gamedev.net from his home office where he lives with his wife and daughter in their home in the woods near Lake Grapevine in Texas.

quote: Original post by johnhattan

One thing I always make a point of doing is to attach the notation to the variable, not the type.

Rather than
String * pString;

I have
String *pString;

Just makes it a bit more obvious to me. Some books seem to do the opposite, putting the adornment on the type, which is downright confusing.


Well, your way makes it more difficult to make a mistake in declaring pointers, which is good. But the other way (String* pString ) is equally valid as it follows the syntax of any other (ie. non-pointer) definition, which is type on the left (in this case, pointer to string), name on the right (pString). Personally I prefer this way, as my variable is of type String*. It just means I have to declare everything on separate lines to be clear.
My C++ book suggests the asterisk near the variable, but it''s a personal decision. I still make that error sometimes myself *sigh*



#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.

This topic is closed to new replies.

Advertisement