( title is: Assigning NULL to a variable (not pointer)
I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?
Basically, you're lying about the meaning of the variable then. If I saw
the code:
int MyVar = NULL;
I would immediatly presume that MyVar is being cast to a pointer somewhere
and is not acutally used as an integer. And I would start reading the code
with that in mind.
I once had someone send me some code to review and I saw things such as:
SOCKET MyVar, MyVar2;
and I knew this code had nothing to do with sockets and started searching
the code to see where it used sockets, and never could find it. Then I got
an inspiration, and went to the definition of the SOCKET var and saw that it
was a long unsigned int, and the progrrammer just wanted a long unsigned
int, and since SOCKET was defined as that he used it. He lied in his code,
which only causes confusion.
NULL is just a shortcut for 0 in C++. But we still use NULL instead of 0
because it has a special meaning for us, that it's used for pointers. If
you use NULL for something that's not a pointer, you are saying that somehow
you are going to be using this variable as a pointer. If you're not, don't
use NULL.