Pointer Syntax

Mar 14, 2013 at 9:01pm
What is the difference, if any, between int* i=0; and int *i=0;.
Mar 14, 2013 at 9:02pm
absolutely nothing. They mean the same thing, it's preference where you place the asterisk. I think since the data type (int) is what is being pointed to, the asterisk should be touching it; int* i = 0
Last edited on Mar 14, 2013 at 9:06pm
Mar 14, 2013 at 9:07pm
Thanks, I had a feeling it was the same thing.
Mar 14, 2013 at 9:51pm
...I think since the data type (int) is what is being pointed to, the asterisk should be touching it...

Consider this.

1
2
3
4
int *x, *y, z;
x = new int;  //OK
y = new int;  //OK
z = new int;  //NOT OK 
Last edited on Mar 14, 2013 at 9:51pm
Mar 14, 2013 at 11:19pm
Good point.
Mar 14, 2013 at 11:22pm
Consider this:

Declaring multiple vars on the same line (especially with pointers) is a bad idea.

I agree that it's more clear for the * to be touching the type, since it indicates the type. But of course that's personal preference and there are many who disagree.
Last edited on Mar 14, 2013 at 11:23pm
Mar 14, 2013 at 11:35pm
Throwing my opinion out; I prefer the asterisk to be touching the variable name. The way I was able to wrap my mind around it in the beginning was thinking of int as the type, and the variable as the pointer - so the asterisk should be touching the variable. I sort of stuck with that ever since.

Disch wrote:
Declaring multiple vars on the same line (especially with pointers) is a bad idea.
Is this for any reason other than readability? Curious.
Mar 15, 2013 at 12:09am
The way I was able to wrap my mind around it in the beginning was thinking of int as the type, and the variable as the pointer


That doesn't really make sense though. If the variable is a pointer, then that means the type is a pointer. If the type is an int, then you don't have a pointer.

Is this for any reason other than readability?


Nope. Just readabilty. Mainly for the reason booradly60 mentioned.
Last edited on Mar 15, 2013 at 12:14am
Mar 15, 2013 at 1:37am
Eh, well that's the only way I could get myself to understand it 3 years ago. The convention just stuck with me.
Topic archived. No new replies allowed.