-2

ok, I know that

int *ptr;

is a pointer to/of an int

.

and if I were to

int i = *ptr;

I am dereferencing that pointer to a value

.

but my question is, WHY is it so common to use

int *ptr;

and not

int* ptr;

instead? they're essentially the same thing, but the first example kinda looks like a dereference rather then a pointer declaration, whereas the second example is a very clear definition of a pointer.

Xyphos
  • 215
  • 2
  • 8
  • 2
    `int*` is confusing. Imagine `int* a, b;` Isn't it strange that `a` will be a pointer and `b` won't? But `int *a, b`, while more obvious, does indeed look like dereferencing. That's why I use `int * a`, but this question is very opinionated. – nikolas Sep 26 '13 at 13:13
  • 2
    Is it really so common to use `int *ptr;`? I hardly ever see that form, which is a good indication that this isn't a good question for SO. It is useful when you declare many variables in one line, but that is often considered poor style. Edit: what I said applies to C++. – juanchopanza Sep 26 '13 at 13:13
  • 2
    This is pretty much a C++ vs C thing, C++ programmers tend to prefer `int* ptr` since this emphasises the type of `ptr`. – filmor Sep 26 '13 at 13:16
  • 1
    This is a prime example of an opinion based question. Like spaces before after parenthesis, curly braces on the same line or next, indenting the curling brace of on the next line, etc... – RedX Sep 26 '13 at 13:16
  • possible duplicate of [difference between int\* i and int \*i](http://stackoverflow.com/questions/3770187/difference-between-int-i-and-int-i) – juanchopanza Sep 26 '13 at 13:16
  • 3
    @nijansen: Re: "int* is confusing.": Says you. – John Dibling Sep 26 '13 at 13:16
  • @nijansen, your example looks like a multiplication at first sight. – Shoe Sep 26 '13 at 13:16
  • @nijansen The only confusing thing in the examples you showed is that you declare several variables (`a` & `b`) at the same time. – syam Sep 26 '13 at 13:18
  • I disagree that this is opinion or preference based. Whether you choose to do one or the other is opinion based, but the reasons for doing either are pretty much well presented here already, in a neutral manner. – Magnus Hoff Sep 26 '13 at 13:19
  • I like to see the declaration as `*p` is an `int`, i.e., declare variable `p` such that `*p` is of type `int`, i.e., declare a variable `p` of a type that points to an `int`. That's why I prefer `int *p;` – filipe Sep 26 '13 at 13:19
  • @JohnDibling Yes my comments do express my personal opinions ;) though they **are** right, you know. – nikolas Sep 26 '13 at 13:19
  • Both are quite common, and one could argue a case for using either, if one particularly wanted to imbue whitespace with some kind of meaning. Just write whichever you think looks nicer, and be prepared for others to lay their code out differently. – Mike Seymour Sep 26 '13 at 13:23
  • You can always do `int * ptr`... the compiler will bind the `*` to the right, so to the compiler they are equivalent but it looks more like `int *p` than `int* p`. This is only noticeable when you declare multiple variables in the same line: `int* p,q;` might look as declaring both `p` and `q` as pointers to int, but it really means `int (*p),q;` (only `p` is a pointer). Many coding guidelines forbid multiple declarations in the same statements, some mandate the placement of the `*`... when in Roma do as Romans (i.e. follow the conventions used in the code you are working with) – David Rodríguez - dribeas Sep 26 '13 at 13:25
  • `int *p;` is common use in C, because it is the expression `*p` which has the type `int`. In C++, `int* p;` is more frequent, as C++ deals more with types, and the type of `p` is `int*`. (K&R use `int *p;`, Stroustrup uses `int* p;`.) – James Kanze Sep 26 '13 at 13:37
  • @filipe That's the origin of the syntax. It stopped working with the introduction of `typedef` and `struct`. And later `const` and `volatile` and user defined types. – James Kanze Sep 26 '13 at 13:39
  • I use `int* p`. One of my coding standards is to never, _ever_ define two variables in the same line, so I never have the confusing `int* p, q;`. Actually, I will _always_ initialize `p`, so I'll have something like `int* p = NULL`, rather than a mere `int* p;`. I have no problem with `int * p = NULL;`, but I dislike `int *p = NULL;`. If you argue that this is a matter of taste, you will be right. – Daniel Daranas Sep 26 '13 at 14:09

3 Answers3

4

Because the first option is more syntactically consistent with the language:

int *a, *b; // they are both pointers
int* a, b; // only a is a pointer

Secondly, C programmers tend to think in terms of data, so it can feel natural to associate the pointer attribute to the variable name instead of the variable type. C++ programmers tend to think in terms of types instead, so you may see the second notation more often in C++ code.

The two notations are equivalent, but depending on how you perceive pointers in a given language you may want to use one notation or the other (or a different one altogether).

Thomas
  • 3,321
  • 1
  • 21
  • 44
  • hm, I never considered that. also, I would have probably made the mistake of assuming that "b" was a pointer too. thanks for teaching me something new. – Xyphos Sep 26 '13 at 13:26
3

The first form is used because it gives a better visual cue when declaring multiple variables at once:

int *ptr, actualInt;

To declare two pointers you would do:

int *ptr1, *ptr2;

Comparing with

int* ptr1, ptr2;

It is not so obvious that ptr2 in the latter has type int. Of course you can avoid this with a typedef:

typedef int* IntPtr;

IntPtr ptr1, ptr2;

Now both are int*. To avoid confusion stemming from this you should avoid declaring multiple variables with pointer types like this altogether (or at least use the typedef variant to make it clear).

If you only have one variable it is pretty much a matter of taste as the others have already said.

Either you want to group all type information at one place (int* var)or you want to emphasize that a variable is a pointer (int *var).

0

They mean the same thing. It's a matter of personal preference.

Nikola
  • 817
  • 13
  • 19