Take the 2-minute tour ×

I vaguely recall seeing this before in an answer to another question, but searching has failed to yield the answer.

I can't recall what is the proper way to declare variables that are pointers. Is it:

Type* instance;


Type *instance;

Although I know both will compile in most cases, I believe there are some examples where it is significant, possibly related to declaring multiple variables of the same type on the same line, and so one makes more sense than the other.

share|improve this question
possible duplicate of Pointer declarations in C++: placement of the asterisk –  Qadi Nov 4 '14 at 18:17

8 Answers 8

up vote 16 down vote accepted

It is simply a matter of how you like to read it.

The reason that some people put it like this:

Type *instance;

Is because it says that only instance is a pointer. Because if you have a list of variables:

int* a, b, c;

Only a is a pointer, so it's easier like so

int *a, b, c, *d;

Where both a and d are pointers. It actually makes no difference, it's just about readability.

Other people like having the * next to the type, because (among other reasons) they consider it a "pointer to an integer" and think the * belongs with the type, not the variable.

Personally, I always do

Type *instance;

But it really is up to you, and your company/school code style guidelines.

share|improve this answer
This was it, I think. It was the declaring multiple variables in a row, and I couldn't remember whether the compiler treated "Type*" as a type, or whether the * was more like a modifier on a variable stating it was a pointer to an instance of type "Type". int* a,b,c would be confusing to me, so I'll use Type *instance in the case of there being no other guidelines. –  Edd Jul 19 '10 at 11:58
I use int * in C and int* in C++. In C++ it is generally good practice to declare identifiers one by one, so the argument "int* a, b, c is fluffy" is less meaningful. Type system consistency is much of a concern in the C++ case. –  Alexandre C. Jul 19 '10 at 13:01

Those two are the same. However, if you do multiple declarations, the former can trap you.

int* pi, j;

declares an int pointer and an int.

share|improve this answer

Those two are exactly the same.

However, for the purposes of declaring multiple variables, as in below:

int * p1, p2;

where p1 is of type pointer to int, but p2 is of type int. If you wished to declare p2 as a pointer to int, you have to write:

int *p1, *p2;
share|improve this answer
This is true, in the single-declaration case that the OP has, but not in the general case. –  Puppy Jul 19 '10 at 11:57
@DeadMG: that's correct, edited to clarify. –  Michael Foukarakis Jul 19 '10 at 12:07

It's an accident of C syntax that you can write it either way; however, it is always parsed as

Type (*pointer);

that is, the * is always bound to the declarator, not the type specifier. If you wrote

Type* pointer1, pointer2;

it would be parsed as

Type (*pointer1), pointer2;

so only pointer1 would be declared as a pointer type.

C follows a "declaration mimics use" paradigm; the structure of a declaration should mimic the equivalent expression used in the code as much as possible. For example, if you have an array of pointers to int named arr, and you want to retrieve the integer value pointed to by the i'th element in the array, you would subscript the array and dereference the result, which is written as *arr[i] (parsed as *(arr[i])). The type of the expression *arr[i] is int, so the declaration for arr is written as

int *arr[N];

Which way is right? Depends on who you ask. Old C farts like me favor T *p because it reflects what's actually happening in the grammar. Many C++ programmers favor T* p because it emphasizes the type of p, which feels more natural in many circumstances (having written my own container types, I can see the point, although it still feels wrong to me).

If you want to declare multiple pointers, you can either declare them all explicitly, such as:

T *p1, *p2, *p3;

or you can create a typedef:

typedef T *tptr;
tptr p1, p2, p3;

although I personally don't recommend it; hiding the pointer-ness of something behind a typedef can bite you if you aren't careful.

share|improve this answer
Hiding the pointer-ness of something with a typedef can be quite handy when using smart pointers. Writing MyClassPtr is much quicker than boost::shared_ptr<MyClass>, for example. –  Pedro d'Aquino Jul 19 '10 at 15:03
It's also a pain in the ass when you have to trawl through several hundred lines of template diagnostics because an iterator doesn't get dereferenced properly; i.e., you were supposed to write (*it)->foo() instead of it->foo(), but because the iterator was declared as vector<a_typedef_name_that_doesn't_indicate_pointerness_AT_ALL>::iterator instead of vector<T*>::iterator, you didn't realize the extra dereference was necessary. That was not the most productive afternoon. –  John Bode Jul 19 '10 at 15:43

I prefer the following style:

Type *pointer;

Why? Because it is consistent with the mindset the creators of the language tried to establish:

"The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear."

(The C Programming Language by Brian W. Kernighan & Dennis M. Ritchie, ansi c version, page 94)

It's easy to write FORTRAN in any language, but You always should write [programming language X] in [programming language X].

share|improve this answer

Curiously, this rule doesn't apply if the type is deduced in C++0x.

int a;
decltype(&a) i, j;

Both i and j are int*.

share|improve this answer
I don't think you need C++0x for such a behavior. Simply make a typedef int *IntPtr;, then IntPtr i, j; shows the same behavior. –  Philipp Jul 19 '10 at 12:21

As a side note, I think it helps to understand the motivation behind the C declaration syntax, which is meant to mimic how the variable could be used. Some examples below:

  • char *x means that if you do *x, you get a char.
  • int f(int x) means that if you do e.g. f(0), you get an int.
  • const char *foo[3] means that if you do e.g. *foo[0] (which is the same as *(foo[0])), you get a const char. This implies that foo must be an array (of size 3 in this case) of pointers to const char.
  • unsigned int (*foo)[3] means that if you do e.g. (*foo)[0], you get an unsigned int. This implies that foo must be a pointer to an array of unsigned int (of size 3 in this case).

Loosely, the general syntax is hence [what you get] [how you get it]. The trick is that this can be extended to [what you get] [how you get it], [how you get it], ... to declare multiple things at once. Hence, the following three declarations --

int *p;
int f(int x);
int a[3];

-- can be combined into a single line as follows (and yes, this even works for function declarations. They are not special in this regard):

int *p, f(int x), a[3];
share|improve this answer

All of these will compile, are legal and are equivalent:

Type* instance;
Type * instance;
Type *instance;

Pick a style and be consistent.

share|improve this answer

Your Answer


