Determines whether the pointer is null.

Source: Internet
Author: User

These two days I have been confused about how to judge whether the pointer is null. there are usually two methods (assuming P is a pointer ):

if (NULL == p) {
    // ...
}
if (!p) {
}

Which method is better? This issue should be considered from two aspects. This article focuses on these two issues and is limited to the C language.

Are they equivalent in terms of semantics and compilation?

Are they equivalent to readability and maintainability?

1. Is there implicit type conversion?

C programming language (Chinese Version 2, 177 page), for operators = and! =, Can be compared as follows: the pointer can be a constant expression with a value of 0 or a pointer to void for comparison. Null is defined as (void *) 0 in C, so null = P can be directly performed.

The 175 page also specifies the operator! Must be an arithmetic type or pointer. If the calculation component is equal to 0, the result is 1. Otherwise, the result is 0. The result type is int.

From the analysis above, we can see that none of the above two conditions requires implicit type conversion.At the same time, I tested it under vc2008. In both cases, the compilation forms are as follows:

   1: 00971578  cmp         dword ptr [p],0 
   2: 0097157C  jne         test1+0A3h (971583h) 

Further, the c99 standard = and! There are also provisions, which are consistent with the description in C programming language. Section 6.3.5, expressed! E is equivalent to 0 = E. See the following description:

5 The result of the logical negation Operator! Is 0 if the value of its operand compares

Unequal to 0, 1 if the value of its operand compares equal to 0. The result has type Int.

The expression! E is equivalent to (0 = E ).

In section 6.5.9, The = operands on both sides can be pointers on one side and void pointers on the other side. Alternatively, the pointer on one side is a null pointer constant. See the following description:

-One operand is a pointer to an object or incomplete type and the other is a pointer to

Qualified or unqualified version of void; or

-One operand is a pointer and the other is a null pointer constant.

2. Readability

On pages 32-33 of the second Chinese edition of C programming language, operators are introduced! Is recommended! P instead of P = 0. However, it is also mentioned that it may be difficult to understand in complicated circumstances. Personally think ,! The P method can make the code shorter and look concise. null = P always feels strange.

Iii. Conclusion

The above analysis shows that there is no essential difference between the two methods. The specific method depends on the specific logical expression, but on the code readability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.