Sign in to follow this  
AsOne

Pointer declarations ...

Recommended Posts

I've always wondered which is considered more 'correct' ...
int *pointer;
int* pointer;
The first one is the way I learned it, and the way I see most use it. However, the second way, really does make more sense when you think about it. How many people use the first method? How many use the second method? Anyone ever worked at a software company which enforced a certain format? I'm always curious about different formatting people use when programming, just though I'd see what people think of this.

Share this post


Link to post
Share on other sites
This gets asked a lot.

See Bjarne's FAQ:

Quote:
Is `int* p;` right or is `int *p;` right?
Both are "right" in the sense that both are valid C and C++ and both have exactly the same meaning. As far as the language definitions and the compilers are concerned we could just as well say `int*p;` or `int * p;`

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.

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int* p, p1; // probable error: p1 is not an int*

Placing the * closer to the name does not make this kind of error significantly less likely.

int *p, p1; // probable error?

Declaring one name per declaration minimizes the problem - in particular when we initialize the variables. People are far less likely to write:

int* p = &i;
int p1 = p; // error: int initialized by int*

And if they do, the compiler will complain.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I use :

int* myIntPtr;

ie. the 'Ptr' suffix so I know what it is later on in my code.

Mr. Creamy.

Share this post


Link to post
Share on other sites
I recently heard an interesting argument for using the int * pointer method. Neither the int *pointer or int* pointer methods generalize well when you start adding const or volatile qualifiers to the pointer type and/or the pointed to type, especially when you get multiple layers of indirection. e.g:

int const * volatile pointer;

or even:

int * const * const pointer;

Share this post


Link to post
Share on other sites
I use, and always have used:
int *ptr;

Just a matter of style and avoiding mistakes when I was younger:
int* ptr1, ptr2; // looks like both would be pointers, where they aren't
int *ptr1, *ptr2; // both pointers; what you would (presumably) want

That's just how I do it, though.

Share this post


Link to post
Share on other sites
it was

type * var;

for me but lately i use

type *var;

becaes it is handy in multiple declations

as a theroethical point -

how is a reference to a pointer declared?

int & * p;
int * & p;

i'll probably never need to know... but they *both* look wrong to me


Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
I recently heard an interesting argument for using the int * pointer method. Neither the int *pointer or int* pointer methods generalize well when you start adding const or volatile qualifiers to the pointer type and/or the pointed to type, especially when you get multiple layers of indirection. e.g:

int const * volatile pointer;

or even:

int * const * const pointer;

Or mutable. Or auto. Or register. [grin] There are loads of interesting but mostly useless keywords (well, mutable ins't useless, it's just not very useful) that don't go well with int* pointer and int *pointer..

For the record, I put the asterisk inbetween, with a space on either side.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirLuthor
Or mutable. Or auto. Or register. [grin]

mutable, auto and register are non-issues since they don't modify types, they modify variables, so they don't need to be placed on the right hand side of the *.

Share this post


Link to post
Share on other sites
I've seen int * pointer before, but then it sort of looks like you're multiplying something. Makes me wonder why the * symbol was chosen and not something else for pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by AsOne
I've seen int * pointer before, but then it sort of looks like you're multiplying something. Makes me wonder why the * symbol was chosen and not something else for pointers.


the number of repetions of many characters in c ( and c++ ) is weird - particulary '(' and ')'

function arguments, function calling, c-style casts, sizeof( ), some other stuff i can't remember...

and '&'

references - address operator - bitwise anding - logical anding

looking at my keyboard - almost any printable character has been abused like so.

what alternative was there?

maybe '¬' (unless that is being used...)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this