Which notation do you use or prefer to use to define pointers?
int* x;
or
int *x;
I have heard that the former one makes more sense since we are declaring an integer pointer and the asterisk should attach to int keyword. The problem is that we can't define two pointers like this:
int* x, y;
the incorrectness of the above code may indicate that attaching the asterisk to the variable name is more appropriate as we can define two pointers this way:
int *x, *y;
Another point to make is that if I use a typedef:
typedefint* intP;
then I can define two pointers like this without any problem:
My preference is int *x with asterisk bind closer to the variable. However if you are doing software maintenance you need to be comfortable with all the above syntax in order to understand other ppl software before you can make changes.
Where did I say it is a law?
This is just my preference for better code readability.
This is a concretization of a more general rule of mine: 1 line = one statement
I think that the answer is that both are acceptable depending on what you are trying to do in each case. I have no problem with declaring variables of the same type on a single line. However I think that many coding standards that I have had to comply with suggest one declaration per line.
int x(0), y(0); // I have no problem with this personally.
int* x(0), y(0); // this I have a problem with. It incorrectly leads the reader to believe that y is a pointer when it is not.
You can avoid the problem by not mixing pointer and non-pointer declarations on the same line or by associating the * with the variable instead of the typename. Either guideline should result in code that is easy to read and understand. In some cases you may have to do it however your local coding standards say that you are to do it otherwise your code could be rejected in peer reviews.
int* x;//I prefer this version, because it's declaring
//the variable "x" as type integer pointer
//When I declare multiple pointers, I do this:
int* x1;
int* x2;
int* x3;