which notation do you use for pointers?

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:

typedef int* intP;

then I can define two pointers like this without any problem:

intP x, y;
Last edited on
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.
closed account (EzwRko23)
My preference is:

 
int* x;


and:
1. never declare two variables in one line
2. always assign an initial value on the declaration


never declare two variables in one line


Would u please tell me why?
xorebxebx wrote:
1. never declare two variables in one line
.
Where in the C++ standards will I find this this law??
closed account (EzwRko23)
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
Last edited on
It's not in the standards, is a notational style
I know, it was just the rather imperative tone.
I agree with xorebxebx.

EDIT: note that saying int* a; emphasizes the fact that a's type is not int, but pointer to int.
Last edited on
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.
"Don't sweat the small stuff."
1
2
3
4
5
6
7
8
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;
+1 PiMaster, I don't really like multiple declarations like this: int x, y;.
Topic archived. No new replies allowed.