2

So I came across this little snippet of code from a function

void remove(Node * & p, const int & key){
  if(!p){
    throw out_of_range("The key you're looking for can't be found\n");
  }
  if(p == nullptr){
    return;
  }

and I don't quite understand the difference between !p and p == nullptr. From what I have read about C++ it always seemed to be like these two statements are equivalent. Are they not? Am I wrong?

Need some clarifictation.

Thanks!

4
  • Interesting question in context because p in the code is a reference to a pointer. Does this make a difference? Commented Feb 23, 2020 at 18:07
  • 1
    Is there really nothing in between the first and the second if in the code snippet? Commented Feb 23, 2020 at 18:11
  • @t.niese Nope, nothing. Commented Feb 23, 2020 at 18:16
  • They are the same. Don't look for logic where you did not put it. Commented Feb 23, 2020 at 18:16

3 Answers 3

2

Actually both are same. You can check whether the pointer is null or not using both.

nullptr was introduced in C++11 which can be used like if(p == nullptr) which is preferred over if(!p)

Hope this helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thanks! Everyone keeps saying it's the same. Seems that the person that wrote that code just made a mistake or was extra paranoid :)
They may ask these types of questions in MCQ's such like which if block will be executed ;)
1

The first "if" actually compare if p == 0 (in other word !p). It is kind of C's NULL bridged over into C++ could cause ambiguity in overloaded function. For example: check(int) and check(int*).

The second if is checking p against pointer type (nullptr)

1 Comment

Sorry, but it's still not really clear for me (I am new to C++). What's the difference between checking if a pointer is null and checking if the pointer type is "nullptr"... sounds the same to me.
1

First of ALL

  • Both are 100 % same, Same meaning Same Working
  • Developer have just used 2 If Statements(first one to show a message that it is not found) and 2nd One to Return Back, While he can do both in 1 If as well, Maybe he just wanted to show that he knows both approaches that's why he did it that way Else there is no difference in Both and that it might have taken one more BIG O time. -For More clear view you can Visit this link (difference between p == NULL and !p in c++)

DETAILED ANSWER IS BELOW:

!p and p==nullptr both have the same Working The code in your case shows, that if(!p) and if(p==nullptr) both are used just to show, that the Developer know's Both Approaches and he is a senior developer familiar with older versions of C++ Because if he uses

if(!p){throw out_of_range("The key you're looking for can't be found\n");
return;
}
or Uses
if(p==nullptr)
{throw out_of_range("The key you're looking for can't be found\n");
return;}

But in Your case the Only Thing developer did he Showed the Message that Node not found in First IF And returned the Function in Second If.No matter whatever was in his mind or his intentions were to Do, But Both are Same and its just use of Another IF which is not Needed at All

If you still Required more help about the Detailed Difference Between Both you can check at difference between p == NULL and !p in c++

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.