Placement of the asterisk in pointer declarations Placement of the asterisk in pointer declarations c c

Placement of the asterisk in pointer declarations


4, 5, and 6 are the same thing, only test is a pointer. If you want two pointers, you should use:

int *test, *test2;

Or, even better (to make everything clear):

int* test;int* test2;


White space around asterisks have no significance. All three mean the same thing:

int* test;int *test;int * test;

The "int *var1, var2" is an evil syntax that is just meant to confuse people and should be avoided. It expands to:

int *var1;int var2;


Many coding guidelines recommend that you only declare one variable per line. This avoids any confusion of the sort you had before asking this question. Most C++ programmers I've worked with seem to stick to this.


A bit of an aside I know, but something I found useful is to read declarations backwards.

int* test;   // test is a pointer to an int

This starts to work very well, especially when you start declaring const pointers and it gets tricky to know whether it's the pointer that's const, or whether its the thing the pointer is pointing at that is const.

int* const test; // test is a const pointer to an intint const * test; // test is a pointer to a const int ... but many people write this as  const int * test; // test is a pointer to an int that's const