At 2018-05-05T11:51:00+0100, Keith Marshall wrote: > On 05/05/18 10:48, G. Branden Robinson wrote: > > (Incidentally, I share your preference for putting type qualifiers > > [as opposed to storage classes] _after_ the type name itself. It > > makes complex declarations easier to understand.) > > Personally, I consider that to be a poor choice ... especially if you > are making it on purely stylistic grounds; conventionally: > > const int foo; > > is more common than: > > int const foo;
Yes, it's more common. And ill-advised. > but that's not the real issue. In practice, the placement of "const" > qualifiers is *not* arbitrary; That's true. It largely follows from the English practice of placing adjectives before nouns. That's good comment style but risky C style. > far from "making the declaration easier to understand", it can effect > a subtle change in meaning. Indeed. > For example, > in C code, it is very common to see: > > const char *foo; > > which means something very different from: > > char const *foo; Actually, it doesn't. Try it. > Your stylistic preference might encourage the latter idiom, but it > likely isn't what you meant. (The former declares a mutable pointer to > an immutable C-string; the latter is an immutable pointer to a mutable > C-string). Experiment with the attached program. -- Regards, Branden
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i = 1;
int * ip = &i;
int * const ipc = &i;
int const * icp = &i;
int const * const icpc = &i;
icp = ip;
// Uncomment to attempt assignment to read-only values.
//ipc = ip;
//*icp = 2;
//icpc = ip;
printf("i = %i; ip = %p; ipc = %p; icp = %p; icpc = %p\n", i, ip, ipc, icp,
icpc);
exit(EXIT_SUCCESS);
}
// vim:set cin et sw=4 ts=4 tw=80:
signature.asc
Description: PGP signature
