• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Pointer declaration

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.
 

alain

Older and Wiser
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.
 

DominiConnor

Quant Headhunter
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.
 
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.
 
Top