Re:C-style: My favorite C programming practices
by Someone on Monday May 20, 2024 @03:02AM
(#40413029)
> What's the author's justification? What's your justification?
I’m neither of them, but chances are that’s because you can’t make them left-to-right all the time.
double const *foo; // foo is a pointer to a const double
double *const foo; // foo is a const pointer to a double
compile and do what the comment says; these do not compile: * const double foo; // a pointer to a const double named “foo”
foo * const double; // foo is a pointer to a const double
Re:C-style: My favorite C programming practices
by spc476 on Monday May 20, 2024 @03:42AM
(#40413224)
I use the "right-to-left" style myself. To me, the qualifier (in this case, const), applies to the item to the right. This could be confusing:
const char *const ptr;
The first const applies to the char, but the second one to the pointer itself. Being consistent: char const *const ptr;
The first const applies to the item to its left---char. The second const applies to the item to its left---the pointer. To recap: char *ptr1; // modifiable pointer to modifiable data
char const *ptr2; // modifiable pointer to const data
char *const ptr3; // const pointer to modifiable data
char const *const ptr4; // const pointer to const data
Re:C-style: My favorite C programming practices
by liblfds-temp on Monday May 20, 2024 @02:43AM
(#40412947)
Readability.
C declarations can become unfriendly by being too complex and disordered.
Re:C-style: My favorite C programming practices
by wruza on Monday May 20, 2024 @02:58AM
(#40413014)
Nothing seems wrong with “volatile double pointer as a constant” or “constant character pointer” either, tbh. The way you presented is equivalent, but non-idiomatic, people would stumble upon it often. To become more readable universally this must have been adopted 50 years ago.
> They all read correctly now, when read right-to-left.
... suppose I'm someone who reads from left-to-right, should I flip the order to make it correct for me?