37

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

4

12 Answers 12

82

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.

5
  • 38
    And I'd suggest int * i; is a non-contender only because it looks like multiplication at a quick glance.
    – John K
    Sep 25, 2010 at 20:32
  • 2
    +1 because I didn't consider this scenario before.
    – Pops
    Sep 26, 2010 at 21:18
  • 5
    "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, 2010 at 8:08
  • 2
    @Joren: Sorry, not being a C# programmer, I simply assumed by looking at the code fragment in the question was C or C++. Nov 28, 2010 at 17:51
  • 2
    I think its perfectly reasonable to assume that its C or C++ unless C# is specifically mentioned. One does not see pointers in C# very often. Oct 18, 2014 at 16:22
65

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.

9
  • 8
    I too follow this convention. For the same reason.
    – gablin
    Sep 25, 2010 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, 2010 at 18:18
  • 5
    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. Sep 26, 2010 at 18:17
  • 11
    I would argue that *i has a type of int. Sep 27, 2010 at 1:02
  • 5
    @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, 2010 at 9:56
27

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.

4
  • 2
    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, 2010 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, 2012 at 15:14
  • 1
    For this reason I think they chose to write * next to the variable, because * is an operator applied to it.
    – Giorgio
    Jan 4, 2012 at 15:17
  • I seem to prefer this too - keeps the type definition cleaner. Plus, what Giorgio wrote makes sense to me as well and Dennis wrote something along the lines in his reasoning.
    – shevy
    Mar 30, 2020 at 17:06
9

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.

8

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
6

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.

4

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);
5
  • omg, c++ always shows something new to me :). Thanks! Sep 27, 2010 at 21:58
  • 2
    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, 2010 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. Oct 16, 2010 at 10:33
  • 2
    I know, I was speaking more to the tendency of C++ programs to use language features just because they can.
    – benzado
    Oct 16, 2010 at 21:23
  • @benzado: !!! I have seen this so often!
    – Giorgio
    Jan 4, 2012 at 15:08
2

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.

4
  • 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, 2010 at 0:27
  • @Roger: I know, but this question was specifically about a single variable declaration, and nothing else. Sep 26, 2010 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, 2010 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. Sep 27, 2010 at 11:26
2

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;

2

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.

1
  • 7
    "There are no pointer types in C" -- Where did you get that idea? int* is a pointer type. Jan 26, 2012 at 18:35
1

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.

-2

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

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