Avatar ·

The nullptr keyword in C++: how it's better than NULL and how to use it correctly

Hello everyone. I recently started learning C++ and I'm confused about null pointers. In old textbooks and forums, they often write NULL, but in new examples I see nullptr. Both seem to mean "nothing" for a pointer, but the compiler sometimes complains about NULL.

\n\n

Here's an example that confused me. I wrote a function that takes a pointer to int:

\n\n
void print(int* ptr) {\n    if (ptr == NULL) {\n        std::cout << "Pointer is empty" << std::endl;\n    } else {\n        std::cout << *ptr << std::endl;\n    }\n}\n\nvoid print(int value) {\n    std::cout << "Integer: " << value << std::endl;\n}
\n\n

When I call print(NULL), the compiler (GCC with flag