4 ms·
That's not a constant, that's an immutable variable which is why your diagnostic said it was read-only. const int x = 2; int *p = &x; *p = 3; // Now x
by tialaramex 1y ago
That's not a constant, that's an immutable variable which is why your diagnostic said it was read-only.
const int x = 2;
int *p = &x;
*p = 3; // Now x is 3
And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago, I think we can rule out "unfamiliar with C" as a symptom.
- tredre3 1y agoNow x is 3 but you also get a compiler warning telling you not to do that. In my opinion it's a bit disingenuous to argue that it isn't a const just because you can ignore the compiler and shoot yourself in the foot. If you listen to the compiler, it is reflected in the assembly that it is a constant value the same as #define x 2. Is Rust better at enforcing guarantees? Of course. Is `const` in C `const` if you don't ignore compiler warnings and errors? Also of course. > And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago Ditto!