which notation do you use for pointers?

Sep 14, 2010 at 8:48am
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 Sep 14, 2010 at 8:49am
Sep 14, 2010 at 9:11am
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.
Sep 14, 2010 at 10:46am
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


Sep 14, 2010 at 12:45pm
never declare two variables in one line


Would u please tell me why?
Sep 14, 2010 at 12:53pm
xorebxebx wrote:
1. never declare two variables in one line
.
Where in the C++ standards will I find this this law??
Sep 14, 2010 at 1:01pm
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 Sep 14, 2010 at 1:03pm
Sep 14, 2010 at 1:01pm
It's not in the standards, is a notational style
Sep 14, 2010 at 7:51pm
I know, it was just the rather imperative tone.
Sep 14, 2010 at 7:57pm
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 Sep 14, 2010 at 8:06pm
Sep 14, 2010 at 8:49pm
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.
Sep 14, 2010 at 9:17pm
"Don't sweat the small stuff."
Sep 14, 2010 at 11:48pm
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;
Sep 15, 2010 at 1:29am
+1 PiMaster, I don't really like multiple declarations like this: int x, y;.
Topic archived. No new replies allowed.