liveBook · Manning

Missing value

As explained in Modern C

A null pointer is a pointer value that represents no valid object or function address.

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

How Modern C covers it

How deep it goes
Defines valid, null, and indeterminate states, then connects initialization, truth testing, dereferencing, and undefined behavior.
Examples
Contrasts zero-valued pointers with empty strings and demonstrates direct pointer tests, array boundaries, and NULL expansions.
What it assumes
Assumes pointer variables while teaching their valid, null, and indeterminate states from basic initialization and assignment.
What you can do after
Operate C pointer variables safely by initializing them, testing their null state, and avoiding invalid dereferences.
How it connects
Connects null pointers with pointer validity, pointer initialization, logical expressions, dereferencing, arrays, and undefined behavior.

Also explained in

Compare how 35 books explain missing value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.