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

Started by
16 comments, last by milo2120 23 years, 12 months ago
Whatever you do, never declare two variables on the same line.


- null_pointer
Sabre Multimedia
Advertisement
Looks like you guys forgot about operator precedence! A pointers ''*'' comes way before a comma. That''s why it evaluates the first as a pointer to NODE, but not the second. Maybe ( NODE * ) next, prev; would work ( though it would make for some ugly code... )? I agree with null_pointer, one line per declaration is a good practice It may look innocent declaring more than one variable on a line, but if you get in that habit, it can hurt readability bad.

Dan Arson
Operator precedence doesn''t enter into it. This is declaration syntax, which is an entirely different part of the grammar from expression evaluation. The language (C in this case) was explicitly designed this way so that declarations of variables would mirror their later usage. Thus, just as you write:

*foo

To get the contents of a pointer named foo, you also write:

*foo

To declare a pointer named foo. I''m not saying this is a GOOD thing, as it is the cause of more than a few errors in every programmer''s lifetime. Nonetheless, this is one of the reasons that I''ve fallen into the mode of always putting optional specifiers (like pointer, array, etc...) with the variable name. Which leaves me with a question:

For people that place the ''*'' after the type, rather than before the variable... What do you do with array brackets? Worse, how about arrays of pointers? Just wondering.

-Brian
Double post

I agree that MOST of the time one declaration per line is alright, but imagine doing that with code like this:

int maxXI, maxYI, maxZI, minXI, minYI, minZI;

No pointers there, so no confusion. Wouldn''t do it with pointers, unless I wanted to get a yell from across the room saying something like "HEY, that code you checked in causes a GPF!"


#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.
quote: Original post by osmanb

For people that place the '*' after the type, rather than before the variable... What do you do with array brackets? Worse, how about arrays of pointers? Just wondering.


This makes sense to me:
SomeClass* array[400];

There is nothing to the right of the array brackets to consider binding it to, so there's no complication, as far as I can see? The above is an array of pointers to SomeClass - to put the asterisk on the right would seem to imply (to me) that (a) We are using SomeClass objects, which we are not, and (b) That we are dereferencing 'array', which we are not (at this stage).

Consider: I may wish to typedef my pointers thus:

typedef SomeObject* SomeObjectPtr;

So now, these 2 statements are equivalent:

SomeObject* a_pointer;
SomeObjectPtr a_pointer;

I can now declare an array of pointers just like I could declare an array of any other type:

SomeObjectPtr a_pointer_array[100];

No reason why I should have asterisks hanging around just because the contents of the array happen to be pointer-like.
This enforces my 'belief' that the asterisk is part of the type rather than the variable. Your milage may vary.

Edited by - Kylotan on 4/17/00 11:16:00 AM
Oh, my gawd! I''ve started a reasonable discussion.

Just for the record, I don''t declare multiple variables on one line for readability. Nor do I wish to type the same type name over and over. I use the following format.

int x,
y,
z;

Much neater than either,

int x, y, z;

or,

int x;
int y;
int z;

in my opinion.

Like I said in an earlier post I somehow convinced myself that the C++ syntax interpreted ''int*'' differently from ''int *''.

I concur that it is somewhat clearer to think of it as I am declaring integer pointers (int*) instead of I''m declaring a pointer to an integer (int *). Too bad the syntax doesn''t actually do that though.

And for the record, I''ll be using this format ''int *x'' from now on. Just like I used to.

Mike Roberts
aka milo
mlbobs@telocity.com
quote: Original post by null_pointer

Whatever you do, never declare two variables on the same line.


- null_pointer
Sabre Multimedia


I dunno about the above. I declare more than one variable per line at times when it's best organized (pointers always receive a new line though).


int x, y, z,
max_x, max_y, max_z;

for pointers=

int * px,
* py;

the type def is the way to go though


Edited by - iwasbiggs on 4/17/00 5:29:36 PM
___________________________Freeware development:ruinedsoft.com
Sorry if this has already been mentioned, but you might want to look at a book by Scott Meyers "Effective C++". It contains a lot of those kinds of things you wish other books mentioned, in the form of a bunch of (50) tips chapters. VEry highly recommended...

This topic is closed to new replies.

Advertisement