Archived

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

ELS1

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

Recommended Posts

i typically use: int *i; & void swap(int &a, int &b) BUT in my book it says that Stroustrup introduced this other style and many people adopted it: int* i ; & void swap(int& a, int& b) i know there is no difference..but a matter of style....but what do you like to use and why?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I tend to use int *i;

Makes it easier to read if i have more on a single line like so:

char *t, m;

much simpler to read than:

char* t, m;

The second one makes it looks like both are going to be pointers, while only the first one will be. It''s the variable that is getting assigned to be a pointer, not the "type", so why would you "attach" the * or & to the type, and not the variable? It just makes things confusing, where if you attached it to the variable name, there is no confusion.

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
i prefer "int *i".

however with references i do the opposite; i.e. "const String& s".

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.

Share this post


Link to post
Share on other sites
I usually use

int* i;

and

void afunction(int& one, int& two);

For me it is easier to read that way.
I usually don't do stuff like

int *i, m; Because to me thats just confusing to a reader
no matter how you do it.

EDIT: The reason I do that is because I read them as "___ a pointer to a ____" So in this case, "i is a pointer to an int".

Edited by - xgalaxy on January 11, 2002 1:58:28 PM

Share this post


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

The identation of brackets is just a matter of taste. But as for * I think there is more than just taste because it defines the type of the var so it must not be confusing with other variables.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Definitely int *i. As mentioned above, the ''*'' is a property of the variable, not of the type. If you want an abstracted pointer type, you can still create something like MS''s LPxxx stuff. But writing int* blah is just confusing.

- AH

Share this post


Link to post
Share on other sites
I don't think the '*' makes the property of anything. Doing 'int* i;' and 'int i;" make two variables of two different types.

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.

I have read some people make a difference when writing the return type of a function and the type of a variable (when they are pointers). I don't see any difference, so I write them the same way.

EDIT - we must not let this thread become a war, so I will stop posting my tastes/opinions.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

Edited by - Blaster on January 11, 2002 2:14:22 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I use "int* p" because p is an "int*".

Stroustrup also stresses one declaration per line so things like "int* p, i" would never happen if you always follow his style recommendations.

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 )!

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites