What is your favorite method to declare a pointer?
int* i;
or
int *i;
or
int * i;
or
int*i;
Please explain why.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityWhat is your favorite method to declare a pointer?
int* i;
or
int *i;
or
int * i;
or
int*i;
Please explain why.
If you write:
int* i, j, k;
you misleadingly suggest that all of i, j and k are pointers to int
.
So I contend it's superior to annex the * to the variable name.
int * i;
is a non-contender only because it looks like multiplication at a quick glance.
int*
is seen as a type, and int* i, j
does declare two pointers. The question could be considered to be incomplete – it depends on the language you're using which styles are reasonable. In C I follow int *x
, in C# I do otherwise.
I prefer int* i
because i
has the type "pointer to an int", and I feel this makes it uniform with the type system. Of course, the well-known behavior comes in, when trying to define multiple pointers on one line (namely, the asterisk need to be put before each variable name to declare a pointer), but I simply don't declare pointers this way. Also, I think it's a severe defect in C-style languages.
For C, where we don't have a strong focus on types, I prefer:
int *i;
Because it has an emphesis on the int
, not the pointer. What is the int
? *i
is the int.
int i = 5
, to get the value of i
, you use the name i
. Likewise, if you have int *i; *i = 5
, then to get the value, you use *i
.
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:
int* a, *b; // not int* a, b;
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:
int* a, b, c; // three pointers
I prefer int* i
(C++-style).
I avoid declaring multiple variables in one statement due to the resulting visual ambiguity (int* i, j
).
See also Bjarne Stroustrup's C++ Style and Technique FAQ for rationales.
If you want to declare multiple variables but don't want to repeat the asterisk:
template <typename T>
struct pointer_to
{
typedef T* type;
};
pointer_to<int>::type p1, p2, p3;
(As you can see inside the struct template, I prefer the int* i
style.)
And here is a more general solution:
template <typename T>
struct identity
{
typedef T type;
};
identity<int*>::type p1, p2, p3;
This one works with any "problematic type", for example arrays and references:
identity<int[10]>::type a1, a2, a3;
identity<int&>::type r1(*p1), r2(*p2), r3(*p3);
typedef int* int_ptr
would have done the trick. Sure, I have to declare a new typedef for different pointer types, but in practice how many will that be? Eight at most?
I'd go for int* i;
since the first part denotes the variable type (pointer to int), while the second part denotes the name (i
). It wouldn't make sense to me that the type is int
and the name is *i
. Also, int * i;
looks a bit like multiplication to me.
In declarations I use int * i;
, you read it as i is a pointer to an integer
.
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: int * i, j;
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.
int*
is a pointer type.
Jan 26, 2012 at 18:35
I actually use all three conventions in specific circumstances. At first glance I seem inconsistent, but...
int *
when the identifier is not present, to visually reinforce that the name is not present.int* intptr
on typedef
s and similar declarations to visually reinforce that it's part of the type. Similarly with function pointer declarations: (int* (*foo)(int))
int *identifier
and class &identifier
on function parameters to visually reinforce that the parameter is potentially a so-called "out" parameter.const int * const * const
whenever I use c-v qualifiers.int * foo;
on local declarations.I guess I am somewhat visually-oriented.
I use int *i
because it's easier to see that it is a pointer, though I don't think it is really matter.
int*i;
- whitespace doesn't grow on trees, you know...