C++ General Question

In the following code what do this line mean? p = (int *) 10;

1
2
3
4
5
int j = 5;
int *p = &j;
int &k = j;
p = (int *) 10;
    cout << " j = " << j << " K = " << k << " *p = " << *p << endl;
Last edited on
It assigns to the pointer p the memory address "10".

So line 5 (where you have *p) will try to access the value stored in memory address 10, which could do all sorts of crazy things including (but not limited to): crashing, producing weird output, screwing over the rest of your program, killing your neighbor's dog (or Schrödinger's cat), and/or causing the Sun to go supernova (?!). (Okay, maybe not the last couple of things, but you get the point)

So in short, don't do it.
Topic archived. No new replies allowed.