I've been reading C++ Primer 5th edition. It mentions that
It is illegal to assign an int variable to a pointer, even if the variable’s value happens to be 0.
I give it a try, and find the following result:
int *u = 0; // success
int *w = 123; // fail
/* compile error:
ptr_test.cc:9:12: error: invalid conversion from 'int' to 'int*' [-fpermissive]
int *w = 123;
*/
int zero = 0;
int *v = zero; // fail
/* compile error
ptr_test.cc:9:12: error: invalid conversion from 'int' to 'int*' [-fpermissive]
int *w = zero;
*/
Can someone help me explain the exact rule of pointer initialization? Why assigning to a int pointer to 0 is fine, but 123 is not ok, though they are both integer? Why does 123 result in a to "*int" cast, while 0 does not?
BTW, I'm using g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0.