So simply put, I am checking if two char* are either nullptr or empty through an if
statement but I get a warning saying I am dereferencing a null pointer.
// mplate is a reference to a class
if ((mplate.m_plate != nullptr || mplate.m_plate[0] != '\0') || (plate != nullptr || plate[0] != '\0')) {
// Do something
}
else {
// do something else
}
So basically i'm trying to say in the if
statement is if either mplate.mplate
or plate
is empty or nullptr
do this otherwise do something else.
Severity Code Description Project File Line Suppression State
Warning C6011 Dereferencing NULL pointer 'make'.
Warning C6011 Dereferencing NULL pointer 'model'.
Warning C6011 Dereferencing NULL pointer 'mplate.m_plate'.
Warning C6011 Dereferencing NULL pointer 'plate'.
Warning C6011 Dereferencing NULL pointer 'plate'.
You are doing something like
if (p != nullptr || *p)
i.e. you are dereferencing only if the pointer is nullptr
. This means you do nothing if the pointer is valid, or you dereference if it's invalid (which is UB).
You need to do a logical and
instead, like this
if (p != nullptr && *p)
i.e. only dereference if the pointer is not nullptr
.