开发者

not equal to 0 checks for non null on pointer?

开发者 https://www.devze.com 2023-02-16 03:18 出处:网络
Where p is the name of a poin开发者_运维问答ter, does p != 0 return a bool that is true where p is null (not pointing at anything) ?Yes.The C and C++ standards specify that the 0 pointer is not a vali

Where p is the name of a poin开发者_运维问答ter, does p != 0 return a bool that is true where p is null (not pointing at anything) ?


Yes. The C and C++ standards specify that the 0 pointer is not a valid memory location.


It specifically checks to see, as your title says, if the pointer is non-null, or is not the value 0x00000000. Your pointer might not actually be pointing at anything valid (e.g., could be a trashed pointer that just points to some random point in memory), but after that check you know it is not NULL.

EDIT :

Wording on the question got changed, but p != 0 will return true if p is not NULL.


Yes, in C++, 0 is the value of the null pointer.


Not pointing to anything means a == NULL.

However, on any modern computer/OS, NULL is reliably defined as 0. Only if you are coding for embedded systems or compliant to very old standards, you should/have to use NULL. But the fact that you are using C++ rather than C already ensures that it is indeed 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消