3 ms·
Rarely does someone mean: int *const foo; It's mostly useless to declare a variable that cannot be assigned. But very often people write const int *
by loeg 7y ago
Rarely does someone mean:
int *const foo;
It's mostly useless to declare a variable that cannot be assigned. But very often people write
const int **foo;
When they intended
const int * const *foo;
- tempodox 7y ago> It's mostly useless to declare a variable that cannot be assigned. So all functional languages (where immutability is the default) are mostly useless?
- loeg 7y agoIf you declare a variable in this way in C, it can never be assigned. Not once — zero times. What is or is not good practice in C has little bearing on or relationship to functional languages, so I'm not sure why you bring them up in this context.
- JoachimSchipper 7y agoFWIW, I find that form quite useful for defining short aliases, e.g. int *const current_foo = &items[i].bar.foo; before some complicated manipulations on current_foo. (A decent (SSA-)optimizing compiler will produce identical code with and without the declaration.)
- loeg 7y agoYeah, this only works if you initialize at the same time as declaration. And as you say, without const, the same code is generated. All it prevents is accidental reuse of current_foo, which may have some value.