Pointer Syntax

What is the difference, if any, between int* i=0; and int *i=0;.
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
Thanks, I had a feeling it was the same thing.
...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
Good point.
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
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.
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
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.