Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

milo2120

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

This topic is 6699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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**

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!