Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is your favorite method to declare a pointer?

int* i;

or

int *i;

or

int * i;

or

int*i;

Please explain why.

see also: http://www.stroustrup.com/bs_faq2.html#whitespace

share|improve this question
18  
I would prefer to focus on the others 3000 lines... –  user1827 Sep 25 '10 at 17:52
22  
int*i; - whitespace doesn't grow on trees, you know... –  Shog9 Sep 26 '10 at 1:12
    
This is essentially the same question; here's my answer. –  Keith Thompson Jan 26 '12 at 18:43
add comment

closed as not constructive by ChrisF Nov 21 '11 at 23:16

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 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, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

12 Answers

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.

share|improve this answer
27  
And I'd suggest int * i; is a non-contender only because it looks like multiplication at a quick glance. –  John K Sep 25 '10 at 20:32
1  
+1 because I didn't consider this scenario before. –  Pops Sep 26 '10 at 21:18
2  
"you misleadingly suggest that all of i, j and k are pointers to int." Unless you're writing C#, where 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. –  Joren Nov 12 '10 at 8:08
1  
@Joren: Sorry, not being a C# programmer, I simply assumed by looking at the code fragment in the question was C or C++. –  Randall Schulz Nov 28 '10 at 17:51
add comment

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 star 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.

share|improve this answer
8  
I too follow this convention. For the same reason. –  gablin Sep 25 '10 at 18:02
5  
Personally, if I'm declaring multiple pointers in my code, I treat it as a code smell. I shouldn't need that many pointers; and usually, I don't. So the issue of multiple declarations on a single line never comes up in any code I write: One declaration per line. –  greyfade Sep 25 '10 at 18:18
3  
The problem with this is that the type is not a "pointer to an int". C (and C++) doesn't have a pointer type. It is a pointer to a block of memory, of which is type int. –  Billy ONeal Sep 26 '10 at 18:17
6  
I would argue that *i has a type of int. –  Tim Goodman Sep 27 '10 at 1:02
2  
@Billy: If so, I've never seen that confusion in 12 years of trying to understand, parse, and explain standardese. "Pointer to an int" is a perfectly valid type in C and C++. –  Roger Pate Sep 27 '10 at 9:56
show 4 more comments

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.

share|improve this answer
1  
Good point. With pointers, you can think of the start as being part of the name. For example, if you have 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. –  mipadi Sep 27 '10 at 15:21
    
I read int *i as: *i is an int. Therefore i is a pointer to int. Variable declarations in C use type expressions, int and * are just operators. int *i parses as int(*(i)) and is interpreted as i has type pointer-to integer. char *p[] parses as char(*([](p))) (because [] has higher precedence than *) and means: p has type array-of pointer-to char. –  Giorgio Jan 4 '12 at 15:14
    
For this reason I think they chose to write * next to the variable, because * is an operator applied to it. –  Giorgio Jan 4 '12 at 15:17
add comment

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.

share|improve this answer
add comment

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

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;

share|improve this answer
add comment

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);
share|improve this answer
    
omg, c++ always shows something new to me :). Thanks! –  Tamás Szelei Sep 27 '10 at 21:58
1  
OMG, C++ is horrifying! 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? –  benzado Oct 14 '10 at 17:54
1  
@benzado In practice, you don't need the typedef or any of the fancy tricks I demonstrated, because no sane person declares multiple variables in one line. Still interesting, though. –  FredOverflow Oct 16 '10 at 10:33
1  
I know, I was speaking more to the tendency of C++ programs to use language features just because they can. –  benzado Oct 16 '10 at 21:23
    
@benzado: !!! I have seen this so often! –  Giorgio Jan 4 '12 at 15:08
add comment

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.

share|improve this answer
    
C declaration syntax isn't consistent in this regard, as type information can easily come after the identifier: arrays being the clearest example. –  Roger Pate Sep 26 '10 at 0:27
    
@Roger: I know, but this question was specifically about a single variable declaration, and nothing else. –  Allon Guralnek Sep 26 '10 at 15:56
    
I think that's reading a bit much into it. :) The way I understand the question, it's asking about which style you use everywhere. –  Roger Pate Sep 26 '10 at 22:09
    
@Roger: You're probably right, but my aim was to answer the OP's question: "What is your favorite method to declare a pointer?". Obviously, everyone is free to take a boarder look at the issue presented in the question, and it is even encouraged here. As someone who doesn't develop in C/C++, I felt doing so would stray too far from my field of expertise. –  Allon Guralnek Sep 27 '10 at 11:26
add comment

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 typedefs 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.

share|improve this answer
add comment

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.

share|improve this answer
2  
"There are no pointer types in C" -- Where did you get that idea? int* is a pointer type. –  Keith Thompson Jan 26 '12 at 18:35
add comment

I use int *i because it's easier to see that it is a pointer, though I don't think it is really matter.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.