This works, came across in a book saying it is danger to use this fashion.But I could not get it why ?..any idea...also p and &x displays different address..(tried it on cygwin)
'p' is a pointer, and you're not telling it to point to anything before you assign what it points to a value.
*p = x;
at that line of code... p hasn't been initialized to point to anything, so it points to nothing (garbage). By doing this line, you're writing 'x' to some random area in addressing space which might cause a program crash or dreaded memory corruption (these bugs are so hard to trace).
If you want to make p point to x... then you want this:
p = &x;
in which case, when you print them like you were with that cout, they will be the same.