Archived

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

flucknugget

how do you make your pointers?

Recommended Posts

flucknugget    122
1) type* name; 2) type *name; 3) type * name; i use #1, i personally think it makes the most sense, since the pointer is kind of part of the type. int* myint would be a pointer to an int. i''m just really curious, how do you guys do it? - f l u c k y p o o - the geek inside

Share this post


Link to post
Share on other sites
clabinsky    122
quote:
Original post by flucknugget
1) type* name;
2) type *name;
3) type * name;

i use #1, i personally think it makes the most sense, since the pointer is kind of part of the type.


Disagree, *name shows that name is a pointer because it is connected to the variable in my opinion. I see your point, but when you declare several variables like:


    
int var1, var2, var3;
int *var1, *var2, *var3;



How would you declare this according to your method?


    
int* var1, * var2, *var3; ??
or you always do:
int* var1;
int* var2;
int* var3;


Just my humble point of view.


[edited by - clabinsky on March 20, 2002 11:44:52 PM]

Share this post


Link to post
Share on other sites
Martee    476
#1 all the way. As flucknugget said, the ''pointer'' qualifier is part of the variable''s type, not the variable''s name.

I find #2 to be misleading. It gives the appearance of dereferencing the pointer:
int *i = &p;
but what''s really happening is
i = &p;

Share this post


Link to post
Share on other sites
Pactuul    122
no clabinsky, you can just do this:

int* var1, var2, var3;

they would all be pointers. I prefer method one also, because of this. Also in my head when I read this I read "integer pointer called var1" not "var1 is a pointer to an integer"

just the way I like it I guess

-Pac

Share this post


Link to post
Share on other sites
clabinsky    122
quote:
Original post by Pactuul
no clabinsky, you can just do this:

int* var1, var2, var3;

they would all be pointers.



Oh no! Then all my projects are wrong...!

Actually, please check your code because, only your first variable will be a pointer - don''t believe me? Try it.

I was hoping someone would write this because your example is the reason most writers emphasize putting the * together with the variable. Otherwise it looks as if all variables declared after the type* are pointers, and that is why it is misleading!

Share this post


Link to post
Share on other sites
ZealousElixir    256
quote:
Original post by flucknugget
int* myint would be a pointer to an int. i''m just really curious, how do you guys do it?



Nehhhh...that''s kinda shaky. It''s a pointer to int, not a pointer to an int. It depends on how you read it. If you say "Foo is a pointer to int," then it makes sense to write "int *Foo", but if you say "Foo is an int pointer," or the like, it makes sense to write "int* Foo." In reality, there''s a problem with every method.
int * Foo; // what if I have multiple declarations here? is each one a pointer? NO.
int *Foo; // probably the best way, but if we try to make it into a declaration list, the 2nd-nth elements are ints, not pointers to int.
int* Foo; // this really messes me up. The type is not "pointer to int," but Foo is a pointer to int. If we make this a list, all following elements will, once again, be non-pointers.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
ncsu121978    1344
i prefer the first method.
but int* var1, var2, var3;
that thing.....only the first one is a pointer
the rest are normal ints.........although a pointer
is really like an unsigned long...which is kinda like a normal int

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
ZealousElixir    256
quote:
Original post by ncsu121978
i prefer the first method.
but int* var1, var2, var3;
that thing.....only the first one is a pointer
the rest are normal ints.........although a pointer
is really like an unsigned long...which is kinda like a normal int


Whaaaa?! Granted it''s the same number of bytes, but that logic is totally off. A pointer stores an address, not a variable. If you think like that, you''re bound to make some wacky errors.

Later,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
Pactuul    122
Well i'll be damned...Int* var1, var2; doesn't work like I said. But then again I don't think i've ever declared multiple pointers on the same line either. I tried that out and got some huge errors. (learn something new everyday, even after 4 years of c++...)

Thanks for the quick correction!
-Pac


P.S. but I still exclusively use int* var1; though. I still read it as "int pointer called var1".

[edited by - Pactuul on March 20, 2002 12:26:05 AM]

Share this post


Link to post
Share on other sites
outRider    852
Usually I use int *ptr, but for casting sometimes (int *) sometimes (int*), for templates,

...Anyways, who gives a flying f%%k...

------------
- outRider -

Share this post


Link to post
Share on other sites
Caesar    126
Once I''ve read somewhere that the int *x; method was supposed to be the right one for pure C while int* x; for C++. Dunno if it''s right. Anyway use the int *x; method...

Share this post


Link to post
Share on other sites
MARS_999    1627
Method #2 is supposed to be C++ style and #1 is C from what I have read. Really who cares.
I personally like #2 due to I don''t like cluttering up my keywords
int *p_int = new int;
or
int a = 0;
int *p_int = &a;
Go figure just pick a style and go with it. =) Just have fun coding!!


Bill Gates is my Pool Boy!!
Nothing is to good for me!!

Share this post


Link to post
Share on other sites
_Jax_    122
I use #1. Pointers are really part of the type (they have different sizes and you need to dereference them). But however I use typedefs if I use it regulary. like:

typedef char *pchar;

pchar name1,name2,name3;

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.