Pointer declaration

  • Thread starter Thread starter Eddie
  • Start date Start date
Joined
5/17/06
Messages
99
Points
26
Just need to confirm that the following two ways of declaring pointers are equivalent:

Type* identifier;

Type *identifier;

They both declares a pointer called identifier of Type , right?. I came across these two kinds of declarations in different books, it used to be really confusing to me, because I got muddled of the latter version with deferencing, which use the '*' in front of the identifier. Please clarify for me. Thanks.
 
Eddie said:
Just need to confirm that the following two ways of declaring pointers are equivalent:

Type* identifier;

Type *identifier;

They both declares a pointer called identifier of Type , right?. I came across these two kinds of declarations in different books, it used to be really confusing to me, because I got muddled of the latter version with deferencing, which use the '*' in front of the identifier. Please clarify for me. Thanks.

I think so. I always used the latter for some reason.
 
Agree with the above, but what you might have read is one of the following

TYPE *p, q;
Defines p to be a pointer, but q isn't

However a typedef would get them both.
 
For whatever reason I find Type *p to be more readable, I guess because it is more common.
 
http://www2.research.att.com/~bs/bs_faq2.html#whitespace
The choice between ``int* p;'' and ``int *p;'' is not about right and wrong, but about style and emphasis. C emphasized expressions; declarations were often considered little more than a necessary evil. C++, on the other hand, has a heavy emphasis on types.
A ``typical C programmer'' writes ``int *p;'' and explains it ``*p is what is the int'' emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.
A ``typical C++ programmer'' writes ``int* p;'' and explains it ``p is a pointer to an int'' emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.
 
Coming from a C++ background (i.e. not specifically C), I always prefer the former approach. As Polter says, it puts emphasis on the type.
 
Back
Top Bottom