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.