DEV Community

Cover image for C/C++ Pointer Alignment Style: A Justification

C/C++ Pointer Alignment Style: A Justification

Jason C. McDonald on February 09, 2019

Virtually all coding style issues are a matter of subjective opinion and personal taste. Tabs v. spaces, Allman v. K&R, operator padding v. non...
Collapse
 
bluma profile image
Roman Diviš

I personally use same format rules as You. But I’m missing one important thing for young c/c++ programmers - information about declaration of multiple pointer variables. Because “int* a, b, c” results in A being pointer. B and C are normal int variables. Thus you should write “int *a, *b, *c” to create three pointers. Basically I don’t use this and prefer to create three separate declarations.

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Thanks for pointing that out! That is indeed an argument for right-alignment, and I found it mentioned in technical documentation elsewhere.

You're also correct, however, that initializing three variables like that is typically bad form anyway. So, one could argue that if you avoid that practice as a rule, left-alignment still wins the day.

Edited the article to include all that. :)

Collapse
 
stefanomontani profile image
stefanomontani

I'm a young c++ programmer and a veteran c# programmer and I have to say that the notation with the asterisk aligned to the left makes more sense to me. Alas, if writing “int* a, b, c” results in the declaration of the first variabile as a pointer to int and of the others two as int variables, from a logical point of view this closes the question for me: the right syntax requires to attach the asterisk to variable (it is no more a problem of alignment). Or the language is flawed (LOL). Bad form is one thing, another thing is wrong syntax.

Though, to follow my preferences and habits (and VC++ default formatting) I think I will use the asterisk aligned to the left and the pratice to write a separate declaration for any variabile.

Collapse
 
jolb profile image
Jolon • Edited

I have to disagree with you regarding center alignment. It seems you have set up a bit of a strawman so you could say that center alignment is ugly.

Center alignment should be written like so

int aVal = 5;
int * aPtr = &aVal;
int & aRef = aVal;
std::cout << *aPtr << std::endl;
std::cout << &aPtr << std::endl;
std::cout << aRef << std::endl;
Enter fullscreen mode Exit fullscreen mode

for the same reason that you wouldn't write std::cout << & aPtr << std::endl; for left alignment.

You mention needing to parse the context to determine if a center aligned variable is being used as an operator, but I think the time spent is negligible because AFAIK you cannot multiply the int type by a value (as in, int * a could never be "int times a" so no time will be wasted parsing it).

As for why I believe center is better than left alignment, consider the following: void func(int* const* a). What is const? The answer is the pointer to the int is const, not the pointer to the pointer, although left alignment seems to (incorrectly) suggest the "outer" pointer is const. Compare this to center alignment and you get void func(int * const * a). No chance of a mix-up here. Now you just need to remember to read right to left: "a pointer to a const pointer, which points to an int".

Obviously, you could remember to read right to left when using left alignment too, but if you don't have enough caffeine, as you mentioned, you might forget this rule and read it incorrectly.

Collapse
 
ravi-prakash-singh profile image
Ravi Prakash Singh
Collapse
 
haujetzhao profile image
豪杰 赵

I'm a begining learner, in my view, the right-align pointer sucks. Really appreciate your post!

Collapse
 
jrbrtsn profile image
John Robertson

It's most important to be consistent. I occasionally use GNU indent to reformat source code.