liveBook · Manning

Const-safety

As explained in Modern C

Const-safety is the property of an interface that preserves read-only qualification when accepting or returning pointers to objects.

Const-safety preserves read-only qualification across an interface that accepts or returns object pointers. For example, numberline declares its input as char const, while fgetline receives writable storage. section 14.1

Read-only input versus writable input

The declaration size_t* numberline(size_t size, char const lbuf[restrict size], size_t* restrict np, int base) communicates that lbuf is read-only to the function. By contrast, fgetline receives char s[...] and writes through it before using strchr to locate and remove a newline. The library interface's lack of const-safeness must therefore be assessed against the actual mutability of the argument.

How Modern C covers it

How deep it goes
Defines the property and applies it to declarations and mutation, but defers broader interface analysis to the programmer.
Examples
Uses numberline's read-only input and fgetline's writable buffer to contrast interface promises with implementation behavior.
What you can do after
Evaluate C function interfaces by checking pointer qualification against the operations performed on their arguments.
How it connects
Ties const qualification to pointers, array parameters, restrict, string mutation, and C library interfaces.

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.