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

Arc
  • 273
  • 2
  • 7
Lesmana
  • 1,559
  • 2
  • 15
  • 18
  • 21
    I would prefer to focus on the others 3000 lines... –  Sep 25 '10 at 17:52
  • 30
    `int*i;` - whitespace doesn't grow on trees, you know... – Shog9 Sep 26 '10 at 01:12
  • [This](http://stackoverflow.com/q/8947260/827263) is essentially the same question; [here's my answer](http://stackoverflow.com/a/8947986/827263). – Keith Thompson Jan 26 '12 at 18:43
  • 1
    [Placement of the asterisk in pointer declarations](http://stackoverflow.com/q/180401/995714), [In C, why is the asterisk before the variable name, rather than after the type?](http://stackoverflow.com/q/398395/995714) – phuclv Aug 02 '16 at 10:03

12 Answers12

83

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.

Randall Schulz
  • 1,902
  • 10
  • 8
  • 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 '10 at 20:32
  • 2
    +1 because I didn't consider this scenario before. – Pops Sep 26 '10 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 '10 at 08: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++. – Randall Schulz Nov 28 '10 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. – alternative Oct 18 '14 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.

Tamás Szelei
  • 7,737
  • 7
  • 38
  • 42
  • 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
  • @greyfade: Had a comment here, moved the rationale to my [answer](http://programmers.stackexchange.com/questions/7305/int-i-or-int-i-or-int-i/7346#7346). –  Sep 26 '10 at 00:31
  • 1
    +1 The other two forms are not even in the ballpark. – Robert Harvey Sep 26 '10 at 01: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. – Billy ONeal Sep 26 '10 at 18:17
  • @Billy ONeal: What would be the difference? – Tamás Szelei Sep 26 '10 at 18:59
  • 2
    @Tamás Szelei: The difference really doesn't matter to someone using the language -- but if you're trying to explain something in terms of the language used in the standard, it can cause confusion. – Billy ONeal Sep 26 '10 at 19:25
  • 11
    I would argue that `*i` has a type of `int`. – Tim Goodman Sep 27 '10 at 01: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++. –  Sep 27 '10 at 09:56
28

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.

alternative
  • 1,542
  • 13
  • 14
  • 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 '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 04 '12 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 04 '12 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 '20 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
Guffa
  • 2,990
  • 20
  • 16
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.

Arc
  • 273
  • 2
  • 7
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);
fredoverflow
  • 6,854
  • 8
  • 39
  • 46
  • omg, c++ always shows something new to me :). Thanks! – Tamás Szelei Sep 27 '10 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 '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
  • 2
    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 04 '12 at 15:08
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.

Lexi Pim
  • 37
  • 1
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.

Allon Guralnek
  • 1,075
  • 7
  • 15
  • C declaration syntax isn't consistent in this regard, as type information can easily come after the identifier: arrays being the clearest example. –  Sep 26 '10 at 00: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. –  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
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;

Tamara Wijsman
  • 8,259
  • 14
  • 58
  • 94
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.

greyfade
  • 11,103
  • 2
  • 40
  • 43
-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.

tia
  • 965
  • 5
  • 9