Search code examples
c++cclion

Is it safe to pass nullptr to C function?


I have some library written in pure C. Now I'm creating some unit tests but the testing library is written in C++ rather than C. When I pass NULL to API function under test CLion gives me a hint to pass nullptr instead of NULL. Is it safe to pass nullptr to pure C function in this case?


Solution

  • Is it safe to pass nullptr to pure C function in this case?

    I think it is. From the C++11 standard (4.10 Pointer conversions):

    A null pointer constant is an integral constant expression ([expr.const]) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

    A nullptr and an intergral constant expression that evaluates to 0 are equivalent for such conversions.