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.
🔒 Locked
Pointer declarations ...
Advertisement
Original Post
I've always wondered which is considered more 'correct' ...
This gets asked a lot.
See Bjarne's FAQ:
See Bjarne's FAQ:
Quote:
Is int* p;right or isint *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 sayint*p;orint * p;
The choice betweenint* p;andint *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 programmerwritesint *p;and explains it*p is what is the intemphasizing 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.
Atypical C++ programmerwritesint* p;and explains itp 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.
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;
int const * volatile pointer;
or even:
int * const * const pointer;
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.
Quote:
Original post by Telastyn
And I do know of companies/courses that have required one or the other as part of their standardized format.
Quote:
Conversations: The Good, the Bad, and the Deprecated by Jim Hyslop and Herb Sutter:
"No. No, no, no. No coding standard worth your time defines such things"
Enigma
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...)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement