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
 
kaan_kayakayakaan02_d8 profile image
kaan kaya (kayakaan02)

I think it is good practice to use right alignment and if I were to call one of the alignments as right, it would be the right alignment.

if you look at int *x;

int *x declares that x is a pointer to an int. This tells the compiler that x stores an address, and when you dereference it with *x, the compiler will treat the pointed-to memory as an integer.

You should think like "*x is an int" and not "x is a *int" because the asterisk binds to the variable, not the type.

I can also say that if you approach pointers like this its also easier to reason and understand them.

Collapse
 
jrbrtsn profile image
John Robertson

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

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!