mardi 11 octobre 2022

what is going on in my code const to non-const assignment in c++

Normally; const to non-const assignments are forbidden for referance and pointer variables but here I force to do it.

    const int a = 5;
    int* ap = (int*)&a;
    *ap = 123;
    cout << &a << " " << a << endl;
    cout << ap << " " << *ap << endl;

the output surprises me because, although ap and &a has the same memory location they have different data stored.

0x7ffc9ab219fc 5
0x7ffc9ab219fc 123

What's going on here?

Aucun commentaire:

Enregistrer un commentaire