Last active
May 24, 2016 13:04
-
-
Save nitzel/183f8402ea6cfcdb7c98c0df74b99900 to your computer and use it in GitHub Desktop.
Zum Thema int ** nach const int ** cast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Zum Thema int ** nach const int ** cast | |
// compile&run: g++ weirdcpp.cpp -std=c++11 -O0 && ./a.out | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
const int v = 5; | |
int * p; | |
const int ** pp = (const int**)&p; | |
*pp = &v; // p = &v | |
p = (int*)&v; | |
cout << v << endl; // 5 | |
cout << *p << endl; // 5 | |
*p = 10; // const-wert über Pointer ändern | |
cout << &v << endl; // 0x7ffc7940a4b0 | |
cout << p << endl; // 0x7ffc7940a4b0 | |
cout << *p << endl; // 11 - warum?! selbe speicherstelle | |
cout << v << endl; // 5 - ?!? | |
cout << &v << endl; // 0x7ffc7940a4b0 | |
cout << p << endl; // 0x7ffc7940a4b0 | |
cout << endl; | |
// kürzere Version: | |
const int x = 5; | |
*(int *)&x = 10; // const-wert über Pointer ändern | |
cout << *(int *)&x << endl; // const-wert über Pointer ausgeben: 10 | |
cout << x << endl; // const-wert direkt ausgeben: 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment