A religious question: int* i; /*or*/ int *i;?

G

Guest

Here my religious answer...............


Typically in my code I write:

int * abc;
char de;
my_type fgh;
int ijkl;
int * mn;
my_long_type opq;
my_type * rstu;
my_long_type * vw;
my_long_type xyz;

My rules (used in the last 20 years):

type variable_name1; /* Only 1 variable per declaration */
type * variable_name2; /* Note the space between type and * */
very_long_type variable_name3; /* All variable-name aligned */


- Dario
 
H

Howard

Ioannis Vranos said:
However if you declare many variables in the same line, the * with the
variable name is the more reasonable and obvious. Also when you declare
a pointer alone, the * going with the variable name is the more
reasonable too. :)

That's where I disagree. I think "int* p" declares p to be a pointer-to-int
much better than "int *p", which looks more like a dereferencing of p.
How do you dereference by the way * p=7; or *p=7; ?

I use *p = 7; , but that's the dereference operator being applied, not a
declaration.

-Howard
 
X

XZ

If you only declare -- and initialise -- variables at the point of first
use, most of these multiple declarations vanish, so the question doesn't
arise ;-).

However, that wouldn't work in C.
 
H

Howard

Ioannis Vranos said:
You have got the main issue. Since int and int * are different types the
standard should require an error. That's why we are here with this
syntax stuff today.


However based on the fact that they are allowed to be declared together,
and the asterisk denotes a pointer variable, while an object next to it
in the same declaration without an asterisk becomes a non-pointer
object, the most rational thing in this kind of declarations is the
asterisk to go close to the pointer variable itself rather than the type.

To me, the rational thing to do is to forbid my staff from declaring
multiple variables on one line in the first place, regardless of their
types. And since I have a staff of one (me), I can enforce that requirement
quite easily! (I rarely disobey myself these days.) :)

-Howard
 
P

Phlip

XZ said:
However, that wouldn't work in C.

As quoted, C has permitted declaration and simultaneous initialization of
variables...

int x = 0;

....since the 1970s.

Were you trying to say something else?
 
R

Rich Grise

Dario (drinking co?ee in the o?ce?) said:
Here my religious answer...............


Typically in my code I write:

int * abc;
char de;
my_type fgh;
int ijkl;
int * mn;
my_long_type opq;
my_type * rstu;
my_long_type * vw;
my_long_type xyz;

My rules (used in the last 20 years):

type variable_name1; /* Only 1 variable per declaration */
type * variable_name2; /* Note the space between type and * */
very_long_type variable_name3; /* All variable-name aligned */

Well, that makes sense, because, after all, abc is a data item whose type
is pointer. *abc refers to its contents. So, strictly, int *abc doesn't mean
anything. Or shouldn't. Well, it was that way 20 years ago, please correct
me if I'm wrong - I haven't read the actual Spec yet. Is this Spec available
on the net somewhere, or do I need to piece it together from tutorials and
NG stuff? Buying a book isn't in the budget this week, for the same reason
that all my time is free for learning stuff. ;-)

But this is a C++ group - are pointers really that rampant? I'd have thought
a reference would be more in the spirit of OOps? But I'm a C++ noob, and
learned to cut-n-paste C by guess & by gosh, with K&R at my side. :)

Thanks,
Rich
 
R

Rich Grise

Ioannis said:
You have got the main issue. Since int and int * are different types the
standard should require an error. That's why we are here with this
syntax stuff today.


However based on the fact that they are allowed to be declared together,
and the asterisk denotes a pointer variable, while an object next to it
in the same declaration without an asterisk becomes a non-pointer
object, the most rational thing in this kind of declarations is the
asterisk to go close to the pointer variable itself rather than the type.
No it's not. The most rational thing is to declare one item, or at most
one type, per declaration, IM!HO.

Cheers!
Rich
 
I

Ioannis Vranos

Phlip said:
XZ wrote:




As quoted, C has permitted declaration and simultaneous initialization of
variables...

int x = 0;

...since the 1970s.

Were you trying to say something else?


He was probably talking about C90 where variable definitions could occur
only in the beginning of a scope.


However in C99 the can be defined anywhere in a scope, as in C++98.
However I still tend to define all objects in the beginning of scopes
with the exception of for loop counters.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

Rich said:
No it's not. The most rational thing is to declare one item, or at most
one type, per declaration, IM!HO.


Why? Do you mean you declare stuff in the style:


int i;
int j;
const int WHATEVER=7;


etc?



Myself use this style:


int i, j, *pName, value;

const int WHATEVER=7;


or


class whatever
{
string firstName, secondName;
// ...
};






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
M

Mabden

Ioannis Vranos said:
Why? Do you mean you declare stuff in the style:

int i;
int j;
const int WHATEVER=7;
etc?

Myself use this style:
int i, j, *pName, value;

const int WHATEVER=7;

Well, I find that sloppy. I use a mixture as follows:

int i, j, k; // loop stuff and temps
int max; // max value of that thing
int *ptr; // reason I'm pointing at you

So in general, throw-away ints are listed together, but stuff that means
something gets its own line with a comment about what it is for.

Remember, people, we are primarily writers. You are generating script for
the next programmer (who may be you) to understand and act upon. Be generous
in explanation. The compiler won't mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top