In C, a null pointer is a pointer value that represents no valid object or function address. Pointers can be valid, null, or indeterminate; keeping these states distinct is essential. section 6.2
Null is not an empty string
The declaration `char const*const p2nothing = 0;` creates a null pointer. By contrast, `char const*const p2empty = "";` points to a valid string containing no characters. The two values are not interchangeable. section 6.2
Use pointer states carefully:
- Initialize or assign a pointer with `0` to make it null. section 6.2
- Test a pointer directly in a logical expression; null pointers evaluate to false. section 6.2 section 11.1.4
- Do not confuse a null pointer with an indeterminate pointer: using an uninitialized pointer produces undefined behavior. section 6.2
- Prefer `0` over `NULL`; the C standard permits several `NULL` expansions, including `0`, `0L`, and `(void*)0`. section 11.1.5
char const* name = 0;
// Do something that eventually sets name
if (name) {
printf("today's name is %s\n", name);
} else {
printf("today we are anonymous\n");
}
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":5,\"ch\":2},{\"line\":5,\"ch\":8}],[{\"line\":7,\"ch\":2},{\"line\":7,\"ch\":8}],[{\"line\":6,\"ch\":2},{\"line\":6,\"ch\":6}],[{\"line\":5,\"ch\":2},{\"line\":5,\"ch\":8}],[{\"line\":7,\"ch\":2},{\"line\":7,\"ch\":8}]]"}
!@%STYLE%@!
Shows a pointer initialized to zero and later tested directly before dereferencing or using its string value. section 11.1.4
A null pointer is safe as a state to track, but it is not a valid object to dereference. Set pointer variables to `0` as soon as possible, and change them only when they point to an object of the designated type. section 11.1.4


































