liveBook · Manning

Missing value

As explained in Modern C, Third Edition

nullptr is the C23 null pointer constant used to represent a null pointer value.

For a pointer, nullptr represents absence: assigning it puts the pointer in the null state rather than making it point to an object. A null pointer is not the same as a pointer to an empty string. section 6.2

Null versus empty

char const*const p2nothing = nullptr; char const*const p2empty = ""; p2nothing has no pointed-to object; p2empty points to a string containing only its terminating zero. In a logical expression, p2nothing evaluates to false. This test identifies the null state, but cannot distinguish a valid pointer from an invalid one. section 6.2

Use the null state deliberately:

  • Initialize pointers with nullptr when a valid target is not yet available. section 6.2
  • Use nullptr instead of NULL; pre-C23 NULL expansions can have different integer or pointer types and may obscure intent. section 11.1.5
  • Check allocation results before using returned storage, because failed allocation produces a null pointer. section 13.1
char const*const p2nothing = nullptr;

Inspect the declaration that explicitly initializes a character pointer to nullptr. section 6.2

How Modern C, Third Edition covers it

How deep it goes
Defines null, valid, and invalid pointer states with examples, but defers most pointer operations to later treatment.
Examples
Contrasts nullptr with an empty string, shows an uninitialized pointer, and checks malloc failure before copying.
What it assumes
Assumes basic variables and strings, then teaches pointer states and safe initialization before deeper pointer operations.
What you can do after
Apply it via nullptr to initialize pointers, test absence, replace NULL, and handle failed allocation.
How it connects
Connects null pointers with pointer initialization, logical evaluation, NULL, dynamic memory allocation, and invalid pointers.

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, Third Edition. The section links point to the parts of the book it draws on.