下面的代碼編譯在C,但不是在C++:工程在C,但不是在C++
int *ptr = 25; //why not in C++?
錯誤
prog.cpp: In function ‘int main()’:
prog.cpp:6:11: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
int *ptr = 25;
但這編譯在C和C++:
int *ptr = 0; //compiles in both
爲什麼分配0工作正常,其他數字不起作用?
到底是什麼錯誤? –
因爲指針上下文中的'0'具有特殊含義。 –
'0'是一個空指針常量; '25'不是有效的指針常量。第一個不應該在C中沒有警告的情況下進行編譯。 –