Hacker News new | past | comments | ask | show | jobs | submit login

It will print 10. There's absolutely no issue there at all. You've changed the value of the pointer which is NOT const.

    const int \*b
Means a pointer to a thing that is const. The pointer itself (which is on the stack in this case) is NOT const.

    const int *b = 9;
    *b = 10;
^^^ This will NOT compile.

    const int b = 9;
    int main() {
       int *a = (int *)b;
       *a = 10;
^^^ This will compile (possibly with warnings). If you run it on an embedded target, this will crash: b will have been put in flash, so trying to write to it is a hard-fault.

Yes, C allows you to do 'unsafe' things with pointers. You aren't going to fix that without throwing away C and starting again.

No I don't want to throw it out, I kinda like C. And you can also do seemingly weird things with it. I was trying to bring some light to how const and immutable could be conflated. A lot of people see const and think, no aspect of this is changeable.

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).

I 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.

I 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.

If you think that though, you haven't got a clear picture of what pointers are and how they work. The pointer itself and thing it is pointing to are two completely different variables in memory. Being unclear about the difference is likely to cause lots of problems when coding C.

Using a pointer to iterate through a string/array/other data structure, that is stored in const is a completely standard thing to do.

I do get what's going on here, maybe poor example. I was trying to show off some confused ways I've seen pointers and const used together, in attempt to confuse people. Some corollary of Poe's law maybe at play.

Applications are open for YC Winter 2020

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact