2

I've been doing this for a while:

int* myIntPointer;
string* myString1,* myString2;

But I've noticed a lot of people do this:

int *myIntPointer;
string  *myString1, *myString2;

Which one is more common? Or at least a good programming practice.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
user2348979
  • 143
  • 1
  • 2
  • 8
  • 1
    `int * myIntPointer;` that is how I roll :) the first looks strange, the second looks fine to me. – Grady Player May 10 '13 at 21:08
  • It really doesn't matter as long as you keep it consistent. – liuyu May 10 '13 at 21:10
  • 1
    I would do `string* myString1; string* myString2;` - one declaration at a time with types that could cause confusion. – zch May 10 '13 at 21:13
  • I usually use the second way because it makes multiple declarations unambiguous: `int* a, b;` vs `int *a, b;` or `int *a, *b;` vs `int* a, * b;` (Some might mistake `a` AND `b` in the first way to be pointers) – GRAYgoose124 May 11 '13 at 16:34

5 Answers5

5

The latter is preferable so that one understand that the pointer is not declared on the class, but on the variable. For example, string* myString1, myString2 declares a pointer to a string (myString1) and a string (myString2). This style is more confusing than the alternative style of string *myString1, *myString2.

isaach1000
  • 1,819
  • 1
  • 13
  • 18
  • This is the reason. +1 – Linuxios May 10 '13 at 21:11
  • Strangely enough I find the 1. version less confusing. `string* mystring` tells you that the variable mystring is of type "pointer to a string". – psibar May 10 '13 at 21:52
  • @psibar that would be true for a single variable, but for more variables, that logic will lead you to believe that `string*` works for all of them, because they are all of type "pointer to a string". – isaach1000 May 12 '13 at 02:30
3

It depends on what you would like to put the focus. Personally I prefer typename* var_name, because it is a variable named var_name of the type typename*. But if you do so, you risk confusion when multiple variables are defined like this:

typename* a, b, c, d; // b, c, d are not of a pointer type
2

Short answer: It doesn't matter.

Long answer: As a C++ programmer, (well first of all, I usually try to avoid pointers as much as possible, but) I'm used to keep the asterisk closer to the type since it's rather part of the type definition (int* p declares a pointer of type int*) and I almost never declare more variables at the same line.

No matter what convention you'll decide to follow, keep your code consistent. Once you pick a style to follow, follow it everywhere.

LihO
  • 41,190
  • 11
  • 99
  • 167
2

If you allow:

 int *myIntPointer

Rather than:

 int* myIntPointer

... Then the C++ compiler will still allow non-pointers and pointers to be declared on the same line, which is very bad practice:

//BAD PRACTICE: Variables of different types on the same line
//It is easy to miss the asterisk if different types are used,
//and is still legal (allowed)
int *p1, i1;
i1 = 5;
p1 = &i1;

It is easier (for me) to "see" the pointer when it is on the left hand side and is next to the rest of the type declaration.

A B
  • 4,068
  • 1
  • 20
  • 23
2

I prefer the first one myself, because I think it shows better what you are declaring. The bad thing about this is when you declare several pointer on the same line.

int *p1, *p2;

is more consistant than

int* p1, *p2;

But I tend to always declare ( and initilize if possible ) my variables on the same line. So I would write

int* p1;
int* p2;

This might be a problem if you are declaring lots of pointers at the same time. But if you need lots of pointers at the same time, you should probably look into putting them in a vector or some other solution. A function or class with lots of named pointers will probably have a higher chance of bugs and errors.


All in all I think this is just a matter of preference. Like most coding style topics the only thing that's important is that it's consistant. With time you get used to any coding style.

olevegard
  • 5,294
  • 1
  • 25
  • 29