What is your favorite method to declare a pointer?
int* i;
or
int *i;
or
int * i;
or
int*i;
Please explain why.
|
What is your favorite method to declare a pointer?
or
or
or
Please explain why. |
||||
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.
|
If you write:
you misleadingly suggest that all of i, j and k are pointers to So I contend it's superior to annex the * to the variable name. |
|||||||||||||||||
|
|
I prefer |
|||||||||||||||||||||
|
|
For C, where we don't have a strong focus on types, I prefer:
Because it has an emphesis on the |
|||||||||
|
|
I have preferred int* i for years. However, there is a strong argument for int *i because when using the former style, you still must remember the multiple declaration rule:
Because you must remember this rule, you don't gain any simplicitly—but I wouldn't say it's more complex, either. Avoiding multiple declarations on one line is just another way to say you remember this rule. The difference between the two styles is moot. Even as I use it, however, it feels a bit silly to pretend C declaration syntax works other than it does, by placing the asterisk next to the type rather than the variable to which it is syntactically bound. I don't buy into that one emphasizes the pointer type (for i) while the other emphasizes the int type (for *i), but that may be that after 15 years of C and C++ use, it just is when I look at it, without having to think about it—something most beginners that ask this question can't yet do. Also, even given my preference, I don't find it awkward to read/write code in the other style. Consistency, bla bla blah. No need to even mention int * i. |
||||
|
|
|
I prefer the first one. It comes natural as being a pointer is part of the type. As I use C#, it handles types in a more intuitive way than C, so there is no problem declaring several pointers in the same statement:
|
|||
|
|
|
I prefer See also Bjarne Stroustrup's C++ Style and Technique FAQ for rationales. |
||||
|
|
|
In declarations I use The pointer contributes to both the type and the variable so it should be in the middle. It's a good thing to avoid declaring multiple things on the same line: |
|||
|
|
|
If you want to declare multiple variables but don't want to repeat the asterisk:
(As you can see inside the struct template, I prefer the And here is a more general solution:
This one works with any "problematic type", for example arrays and references:
|
|||||||||||||||||
|
|
I'd go for |
|||||||||
|
|
I actually use all three conventions in specific circumstances. At first glance I seem inconsistent, but...
I guess I am somewhat visually-oriented. |
|||
|
|
|
There are no pointer types in C! So, "int*" means nothing. The asterisk is always bound to the element written right of it, it belongs to the element right to it. "*i" is an int. And because of *i is an int, it follows that i is a pointer to int. That's the logic behind it and that is why "int *i" is the only possible solution. Everything else is an illusion (which is automatically corrected by the compiler in most cases). In C++ and C# that's something different. But for C there is only one bible: "Dennis M. Ritchie: The C Programming Language". Dennis (R.I.P.!) wrote it: "int *i". There is no need to question this. |
|||||
|
|
I use |
|||
|
|
int*i;- whitespace doesn't grow on trees, you know... – Shog9♦ Sep 26 '10 at 1:12