What is the difference between using:
if (NULL == pointer)
and using:
if (pointer == NULL)
My professor says to use the former over the latter but I don't see the difference between the two.
There is no difference. What your professor prefers is called Yoda conditions also see "Yoda Conditions", "Pokémon Exception Handling" and other programming classics.
It is supposed to prevent the usage of assignment(=) by mistake over equality(==) in a comparison, but modern compilers should warn about this now, so this type of defensive programming should not be needed. For example:
if( pointer = NULL )
will assign NULL to pointer when what the programmer really meant was:
if( pointer == NULL )
it should have been a comparison, Oops. Make this an error using Yoda conditions you will (see it live), with a similar message to this:
error: expression is not assignable
As jrok points out using:
if (!pointer)
avoids this problem all together in this case.
Here is a concrete example of why with a modern compilers we don't need this technique anymore(see it live):
#include <iostream>
int main()
{
int *ptr1 = NULL ;
if( ptr1 = NULL )
{
std::cout << "It is NULL" << std::endl ;
}
}
Note all the warnings:
warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
if( ptr1 = NULL )
~~~~~^~~~~~
note: place parentheses around the assignment to silence this warning
if( ptr1 = NULL )
^
( )
use '==' to turn this assignment into an equality comparison
if( ptr1 = NULL )
^
==
which makes it pretty hard to miss the problem. It is worth noting that in C++ nullptr should be preferred over NULL, you can look at What are the advantages of using nullptr? for all the details.
Note, in C++ there is the unlikely case with operator overloading that there could be some contrived case where they are not the same.
Note, the -Wparentheses warning in some ways forces a style choice, you either need to give up potentially valid uses of assignment in places where the warning is generated, for example if you use -Werror or choose to parenthesize those cases, which some may find ugly as the comment below suggests. We can turn of the warning in gcc and clang using -Wno-parentheses but I would not recommend that choice since the warning in general will indicate a real bug.
if ( n = scanf("%d%d", &x, &y) ) may trigger a warning when the code is correct and working as intended. So the programmer has to either disable the warning, or uglify the code . Personally I find a redundant (....) != 0 more ugly than the Yoda condition.If this course is touting to be C++11 compliant, then what really should be used is not the NULL macro check that has been around forever (at least in the C language lifetime). What should be compared to is the nullptr type. For reference: nullptr and What exactly is nullptr?.
From the cplusplus.com page cited above, as of February 28, 2014:
Null pointer type (C++) Type of the null pointer constant nullptr.
This type can only take one value: nullptr, which when converted to a pointer type takes the proper null pointer value.
Even though nullptr_t it is not a keyword, it identifies a distinct fundamental type: the type of nullptr. As such, it participates in overload resolution as a different type.
This type is only defined for C++ (since C++11).
A short sample in C++11 (clang++, C++Builder 64-bit (based on clang++), Visual Studio 2010 and newer; maybe only 64-bit too, GCC 4.7.3 maybe, not sure, could be GCC 4.8, ...):
if (pointer == nullptr)
// this looks odd, but does compile
if (nullptr == pointer)
If this is a strict C11 course, then it looks like there is no improved way to replace the NULL macro. The C99 standard says the NULL macro can be portably expressed as integer value zero converted implicitly or explicitly to the void* type (from Wikipedia: Null pointer). To the best of my knowledge, the C11 standard has not modified this aspect of the C99 standard.
String pointer = "Some value";
Initially value of pointer is "Some value", so if you mistakenly check it with
if(pointer = NULL) instead of if(pointer == NULL), the null value will be assign to pointer; but in case of (NULL = pointer ) the compiler will never allow you to assign anything to NULL. So keep using NULL == pointer in conditional statements.
if (!pointer).if (pointer=NULL) ...mistake even in code that has nothing to with newbies. For some reason, tools aren't always set up to complain about instances of those bugs. However, I also find thatif (NULL==pointer) ...is ugly enough that I still don't use it, even to prevent those kinds of typo-bugs. My personal preference is jrok's suggestion:if (!pointer).