0
Issue with re-assigning something to the same pointer
#include <iostream> using namespace std; int main() { int*p=0; int x=1; *p = &x; // why can we not re-assign &x to pointer p? cout << p; // outputs error
1 Answer
+ 2
See the error message:
Invalid conversion from int* to int
You are assigning address of <x> (which is a int*) as value for data in memory pointed to by <p>.
*p = &x;
*p -> value pointed by <p> (an int)
&x -> address of <x> (a pointer)
(Edit)
I think you should use NULL in place of 0 in the pointer declaration