Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 ELS1   Members   -  Reputation: 127

Like
Likes
Like

Posted 11 January 2002 - 06:35 AM

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?

Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 06:40 AM

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

#3 Big Sassy   Members   -  Reputation: 310

Like
Likes
Like

Posted 11 January 2002 - 06:45 AM

I totally agree with the AP. It just doesn''t make sense to do it the other way (at least to me anyway).

#4 jenova   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 January 2002 - 06:50 AM

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.

#5 granat   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 January 2002 - 06:52 AM

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

#6 xgalaxy   Members   -  Reputation: 124

Like
Likes
Like

Posted 11 January 2002 - 06:55 AM

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

#7 Blaster   Members   -  Reputation: 124

Like
Likes
Like

Posted 11 January 2002 - 07:05 AM

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

#8 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 07:06 AM

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

#9 Blaster   Members   -  Reputation: 124

Like
Likes
Like

Posted 11 January 2002 - 07:12 AM

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

#10 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 07:17 AM

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.


#11 Sam Gamgee   Members   -  Reputation: 222

Like
Likes
Like

Posted 11 January 2002 - 07:27 AM

I do int *i;

#12 LaughingD   Members   -  Reputation: 151

Like
Likes
Like

Posted 11 January 2002 - 07:31 AM

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

#13 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 07:38 AM

int * i;

#14 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 07:58 AM

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 )!

#15 WayfarerX   Members   -  Reputation: 130

Like
Likes
Like

Posted 11 January 2002 - 08:20 AM

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
------------------------------


#16 Martee   Members   -  Reputation: 476

Like
Likes
Like

Posted 11 January 2002 - 08:31 AM

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 ...

#17 UnshavenBastard   Members   -  Reputation: 335

Like
Likes
Like

Posted 11 January 2002 - 09:56 AM

...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.



#18 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 January 2002 - 10:06 AM

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


#19 jenova   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 January 2002 - 10:30 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS