In C, why is NULL and 0 triggering an if statement

cif statementnull

I have a function that is called by

myStruct *structName = myFunction(0);

The function looks like

myStruct *myfunction(int x)
{

    if ( x == NULL)
    {
        return NULL;
    }

/*rest of code*/
}

Passing NULL or 0 BOTH trigger the if (x== NULL) statement and I can't figure out why. I need to return from the function on NULL but continue if the value is 0. Any change made can only be made within the function itself.

Best Answer

In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect).

In the first case, you can not differentiate between NULL and 0, because they are literally the same.
In the second case, your code will cause a compile error, because you can't compare an integer variable with a pointer.