Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3943202
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:45:14+00:00 2026-05-20T00:45:14+00:00

Simple question: Is if (pointerVar) the same as if (pointerVar!=NULL) ? Also, is if

  • 0

Simple question:

Is if (pointerVar) the same as if (pointerVar!=NULL)?

Also, is if (!pointerVar) the same as if (pointerVar==NULL)?

Give me your most technically correct/pedantic answer. The two statements seem and make sense to operate the same. Is there anything wrong with the former though (besides its slightly lower readability)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:45:15+00:00Added an answer on May 20, 2026 at 12:45 am

    For the most pedantic answer, here’s the relevant sections of the spec.

    First, here’s how if statements work, from §6.4.4:

    The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable implicitly converted to type bool. If that conversion is ill-formed, the program is ill-formed.

    “But how are pointers converted to bools?” you may ask. Well, here’s §4.12.1: 🙂

    An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

    So this means that the statement

    if (ptr)
    

    is equivalent to

    if ((bool) ptr)
    

    which is in turn equivalent to

    if (ptr == NULL)
    

    But what about

    if (!ptr)
    

    Well, the C++ spec, §5.3.1.8, says that

    The operand of the logical negation operator ! is implicitly converted to bool (clause 4); its value is true if the converted operand is false and false otherwise. The type of the result is bool.

    So this means that

    if (!ptr)
    

    is equivalent to

    if (!(bool)ptr)
    

    which is in turn equivalent to

    if (!(ptr == NULL))
    

    which is finally equivalent to

    if (ptr != NULL)
    

    Whew! That was a fun search to do. Hope this answers your question!

    Of course, there is more to this story. NULL is not part of the C++ language; it’s a macro in <cstddef> defined as

    #define NULL 0
    

    This works because the C++ standard defines the null pointer in §4.10.1 as

    A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type

    So to be more correct, I should have been using the numeric literal 0 in the above examples. However, if you have <cstddef> included, then this works out to the same code after preprocessing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question (I hope): I have a simple two column table, a measurement and
Simple question about the order of an IF statement in C#.NET if (Company !=null
Simple question, but one that I've been curious about...is there a functional difference between
Simple question, how do you list the primary key of a table with T-SQL?
Simple question - I've got a bucketload of cruddy html pages to clean up
Simple question that keeps bugging me. Should I HTML encode user input right away
Simple question: Can a swing frame be completely modal ( block all others windows
Simple question, hopefully an easy way and just want to verify I'm doing it
Simple question, spurred on by the removal of the target attribute in HTML 4.0
Simple question: How do I do this on one line: my $foo = $bar->{baz};

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.