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

Started by
17 comments, last by ELS1 22 years, 3 months ago
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?
Advertisement
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
I totally agree with the AP. It just doesn''t make sense to do it the other way (at least to me anyway).
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.
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.
int *i

i is the pointer. The int is just the type the pointer points to.

Edited by - granat on January 11, 2002 1:53:46 PM
-------------Ban KalvinB !
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
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
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
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
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.

This topic is closed to new replies.

Advertisement