Pointers: "type* var" versus "type *var"

Started by
31 comments, last by Madolite 8 years, 2 months ago

What conventions do you guys use for pointers, when and why?

type *name and type &name
- OR -
type* name and type& name

I'd like to hear what you guys like to do and why you prefer one thing over another. I'm asking these weird questions because I'm self-taught and experience a lot of people using either convention or even both, depending on various personal criterias. My eyes are practically bleeding from all the interchanged uses.

And then you got type** name and T&& name. Aaaargh!

Thanks. smile.png

Advertisement
I use 'type* name'. I find it easier to read and the * is a type modifier and should thus be closer to the type in my opinion.

On the other hand there is a significantly higher danger of accidentally going 'type* var1, var2;' and not declaring what you expect to declare. I am however strongly opposed to declaring multiple variables in one line like that so it's not an issue for me.

...higher danger of accidentally going 'type* var1, var2;' and not declaring what you expect to declare. I am however strongly opposed to declaring multiple variables in one line..

I prefer a linebreak between variables as well.

I do whatever the nearby code does in general. next to the type, next to the name, or even with space on both sides, it really doesn't matter to me.

I tend to slightly prefer putting the * or & next to the variable.

Not really sure this is a beginner-relevant topic, moving to general programming.

Unencumbered by other surrounding standards, I prefer spaces on either side of the * or &, so "T * pointer" or "T & reference"

My eyes are practically bleeding from all the interchanged uses\

The sooner you get used to this the better; in the grand scheme of things this is yet another one of those pointless decisions that only matters because it's important to make a decision and stick to it.

Personally I use type* var, because I feel like it's part of the type and I find it aesthetically more pleasing, but one could argue it's more clear and more consistent to write it next to the variable name because:


int* a, b;

Here a is a pointer, but b is not.

Put next to the variable name makes more sense here:


int *a, b;

This makes it clearer that a is a pointer and b is not and it's also more consistent in case b were to be a pointer.

My eyes are practically bleeding from all the interchanged uses\

The sooner you get used to this the better; in the grand scheme of things this is yet another one of those pointless decisions that only matters because it's important to make a decision and stick to it.

I'll get used to it, but it's just nice to get some explicit opinions on the matter. smile.png

type *var

always.

more uniform syntax for stuff like

int a,b;

int *a,b;

vs

int a,b;

int* a;

int b;

also, that's the format my macro processor expects. <g>

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

I put it by the variable: Type *myVar;

Unless there is no variable, then I put it by the type: array<Type*>

This topic is closed to new replies.

Advertisement