Re:The Clockwise/Spiral Rule of C declarations
by clusmore on Sunday October 23, 2016 @07:18PM
(#12775902)
I think the advice that helped me the most was "Declaration follows usage".
int* arr[][10];
If you index twice into arr and then dereference, you'll get an int. So arr must be an array of array of pointer to int.
Re:The Clockwise/Spiral Rule of C declarations
by convales on Sunday October 23, 2016 @07:23PM
(#12775916)
Declaration follows usage is much more easier to follow than the artificial spiral rules, IMHO with a few typedefs the declaration follows usage can make things pretty simple.
Re:The Clockwise/Spiral Rule of C declarations
by dllthomas on Monday October 24, 2016 @06:15AM
(#12777711)
To my mind that's one part of the reason the * belongs next to arr rather than next to int.
The other part is `int* x, y`.
Re:The Clockwise/Spiral Rule of C declarations
by vostok on Sunday October 23, 2016 @07:22PM
(#12775913)
For this reason I strongly prefer writing
char const
rather than const char
Is there a reason to prefer the second version? It's a lot more popular in my experience.
Re:The Clockwise/Spiral Rule of C declarations
by Spivak on Sunday October 23, 2016 @07:57PM
(#12776020)
It's the difference between "declare a constant integer" and "declare an integer constant" and to me the former more accurately represents what you're doing since `const` is modifying `int`, `int` isn't modifying `const`.
Re:The Clockwise/Spiral Rule of C declarations
by kccqzy on Sunday October 23, 2016 @08:36PM
(#12776132)
Putting const on the right makes more sense when you have pointers or references. Then you just always read from right to left: `int const ` is a pointer to constant integer whereas `int const` is a constant pointer to integer.
Also your argument about which modifies which is strongly anglocentric: there are plenty of people whose native language puts modifiers after the things they modify.
Instead, when you write declarations, do it from right-to-left, e.g.:
"argv is an array of pointers to constant characters"It doesn't help with reading, unfortunately.