Advertisement

do u prefer int* i OR int *i??

Started by January 11, 2002 12:35 PM
17 comments, last by ELS1 22 years, 10 months ago
I do int *i;
I use int* p; for two reasons:

1) You are declaring a variable of type (int*). If you have CFoo *p; sizeof(p) is only 4. sizeof(CFoo) will most likely be significantly different than 4, thus the * properly belongs to the type of the variable.

2) If I see int *p, I''m likely to think that I''m dereferencing p (if only for a moment). Using int* p; removes that confusion for me.

Mark

Mark Fassett

Laughing Dragon Games

http://www.laughing-dragon.com

Advertisement
int * i;
hmmm,

first of all, I''m doing it like this:

int * p;

I think this is very easy to read and to recognize as an declaration of a << :: .. pointer to an int .. :: >> ( !!! ).

Because when I dereference a pointer I write something like
this:

*p = VALUE;

This would be similar to the "int *p" version,
especially when initializing *p like this:

int *p = VALUE;

You understand what I mean?


If I had to choose between your two versions I would choose "int* p;", but I would never use
"int *p;" and also I would never use "int * p, i;" ( !!! ).
I don''t like this versions because they seem to be misleading,
especially the one declaring a pointer to an integer and an
integer in the same line ( even though you could use an int as
an pointer theoretically )!
quote: Original post by Blaster
I do "int* i" because the type of the variable ''i'' is ''int*''. Same for references.

I don''t do "char* i, m". I do "char* i; char m;" because ''i'' and ''m'' do not have the same type.


These are my thoughts exactly, too lazy to re-type them .

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
int* i is god.

int *i = &x
I don''t like this because it looks like you are assigning &x to *i. That annoys me ...
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
...just to take part in that nonsense, for it seems to be fun:

I like
int *i, since I can see instantly that i is a pointer,
BUT I rather use

int* Function()
{
}

instead of

int *Function()
{
}

since I can see instantly that the function
returns a pointer to an int,
the *Func() looks a bit strange to me...


Well, how about a compromise:
Let''s just all use int * i;

Okay? ;-)


when I dereference a pointer,
I usually type (*p) = blah; to make clear that''s
a dereferencation,
or, more general, (*(p)) = blah;
, well, it''s called "evil" by some, but sometimes
I use pointer arithmetics (hey, I''m careful ;-),
like (*(p+offs)) = blah;,
and (*(p)) is just a pointer with added nothing.

quote:
What do you do with functions that return pointers? I put the ''*'' beside the ''char'' because that''s the type of the variable the function returns. Putting the ''*'' beside the function name doesn''t seem good and according to the last AP the ''*'' would then be a property of the function or something.


Yes, I do:

int *MyFunction(void)

for a function with return type ''int *'', so I''m consistent in my style. But it is all a matter of personal taste and preference.

quote:
int * i;


lol When I see this one, it looks like you are multiplying a variable called ''int'' with ''i''...

- AH
ok, if someone (programmer) is reading your source code and they can''t understand "int *i", "int* i" or "int * i" then i think we have more problems than just ease of readability. seriously, it''s just a personal choice.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

This topic is closed to new replies.

Advertisement