I don't believe there is any way to pass a C++ standard "null value" (0
, NULL
or nullptr
) to a function and distinguish one from another. It will be output as zero either way. In other words, NULL
is defined to be 0
in the C++ standard, and 0
and nullptr
are interchangeable with regards to value - with only the difference that nullptr
can only be assigned to something that is a pointer.
I wrote this code, and it outputs p=0 q=0
for the first line, and p = zero
and q = zero
for the second line.
#include <cstddef>
#include <iostream>
using namespace std;
int main()
{
int *p = nullptr;
int *q = NULL;
cout << "p=" << p << " q=" << q << endl;
if (q == 0)
{
cout << "q = zero" << endl;
}
if (p == 0)
{
cout << "p = zero" << endl;
}
}
If you want to, you could of course write your own translation for 0
to print NULL
instead. Something like:
if (p)
cout << p;
else
cout << "NULL";
But that gets a bit tedious, so I would just accept that NULL
and 0
are the same thing.
(In code I write, I just use 0
, not NULL
or nullptr
)