3 ms·
Declare all variables/qualifiers right-to-left. Read the type for all the below right-to-left, substituting the word "pointer" for "*". int long long unsign
by liblfds-temp 2y ago
Declare all variables/qualifiers right-to-left.
Read the type for all the below right-to-left, substituting the word "pointer" for "*".
int long long unsigned wibble; // unsigned long long int
double const *long_number; // pointer to a const double
double volatile * const immutable_pointer; // immutable pointer to a volatile double
They all read correctly now, when read right-to-left. It's not just "const" you do this for, as per the advice. Do it for all qualifiers.
- mrkeen 2y agoWhat's the author's justification? What's your justification? > 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?
- liblfds-temp 2y agoReadability. C declarations can become unfriendly by being too complex and disordered.
- wruza 2y agoNothing 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.
- Someone 2y ago> 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
- spc476 2y agoI 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
- Y_Y 2y ago* doesn't (have to) mean "pointer"! It works in simple cases, but I find the consistent thing to do is read it as "dereference". double volatile *(const immutable_pointer); // immutable_pointer is immutable, when you dereference it you'll get a volatile double
- planede 2y agoYes, that's the philosophy around the declaration syntax. The declaration of the pointer ip, int *ip; is intended as a mnemonic; it says that the expression *ip is an int. The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear. This reasoning applies to function declarations as well. K&R C