4 ms·
His point was that you aren't interpreting the `const' the same way the compiler does. To the compiler, this: const int *a; a = NULL; // perfectly OK
by jamessu 10y ago
His point was that you aren't interpreting the `const' the same way the compiler does.
To the compiler, this:
const int *a;
a = NULL; // perfectly OK
*a = 0; // does not compile
Is a pointer to a constant int, not a constant pointer to an int.
You're probably confusing it with this:
int *const a;
a = NULL; // does not compile
*a = 0; // perfectly OK
Which is a constant pointer to a regular int.
Of course you can combine both as follows:
const int *const a;
a = NULL; // does not compile
*a = 0; // does not compile
Which is a constant pointer to a constant int (which, of course, makes no sense at all in this case since 'a' is uninitialized and cannot be initialized without an unsafe cast, but it's a perfectly valid statement in C).
- Terribledactyl 10y agoI think we're in violent disagreement. I fully understand what my code did, but people I've worked with and gone to school have been tripped up by statements like these. Stuff like this is all over exams.
- jamessu 10y agoI agree that we disagree. Your comment elsewhere: > It's all about the underhanded trick(s) with the pointers, yours will never compile. I'm forcing clang to do horrible things. Indicates, to me, that you see this as a strange behaviour that you're forcing the compiler into when in fact this is exactly the intended (and expected) semantics of const.