Hi there,

How do we differentiate between NULL and zero in C++. I have a program in which i assign NULL to num variable but if i apply if condition to check whether it is NULL or zero. Program still gives me result zero.

      //num = 0;
      num = NULL;

      if(num == 0)
        cout <<"num is zero"<<endl;
      else if(num == NULL)
        cout <<"num is Null"<<endl;

Anyone please explain how we apply check on NULL and zero ??

Member Avatar for thendrluca

for what you do num = NULL?
what is num? be more explicit please
i think it doesn't have any sense
if you would have a pointer num
*num = NULL;
but in this case i don't see sense

You shouldn't assign NULL to non-pointer values; the representation of pointer values is implementation defined. For most compilers the conversion will result NULL being converted to 0 as int meaning in your case it's both NULL and 0, but again, don't do it..

#include <iostream>

using namespace std;

int main ()
{
    int num = NULL;

    if(num == 0)
        cout << "num is zero" << endl;

    if(num == NULL)
        cout << "num is NULL" << endl;

    return 0;
}

NULL is just zero for pointers. In actuality, there is no difference in the value. It is merely a convention that you should set and test pointers with NULL, and use 0 for integral type (int, short, etc.). On many compilers, NULL is just a #define for 0. In newer compilers, you should technically use std::nullptr instead of NULL, but it is still gonna be indifferentiable from 0. There is, in fact, a section of the standard that mandates that the two (literal value 0 and NULL or std::nullptr) have the same value (all bits zero).

So, just respect the convention and stick with using NULL or std::nullptr for pointer types, and using 0 for integral types. Things are just clearer that way.

As mike_2000_17, NULL is a macro evaluated to 0, in the older includes you could find:

#define NULL 0

It is used to asign the "null" value to a pointer (mearly a convention), something that would say it's null/void/empty/limbo.

It's not that higly recommanded to asign NULL to non-pointer values, thus 0 suits better.

This is one of the few times C and C++ are different, at least in older versions of C. At one time NULL was defined as (void *)0 e.g.
#define NULL (void\*)0, so it was an error to assign NULL to a non-pointer. Turbo C still uses that since its such an old C compiler. Don't know if the newest C standards kept that or not.

And in segmented compilers such as Turbo C
in Large memory models #define NULL (_FAR void\*)0
in small memory models define NULL (_NEAR void\*)0

When 32-bit compilers and operating systems were invented, the compilers used flat memory models so the definition of NULL was changed to just (void*)0, no mention of memory model because programs were no longer segmented.

Thanks for your replies, but first tell me what is null pointer values ??

a null pointer value means a pointer whose value is set to NULL, for example

char* somepointer = NULL;

You should set pointers to NULL when they don't point to something useful so that you can tell if the pointer has a valid address or not. If you allocate memory to the pointer using the new operator, then later delete it you need to set the pointer to NULL to insure you don't attempt to access the deleted memory again. Failing to do that is the cause of many bugs and hours/days of hunting down programming errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.