liveBook · Manning

Read-only object

As explained in Modern C

A read-only object is an object that cannot be modified through the applicable C access or type qualification.

A const-qualified object is read-only: code using that qualification does not have the right to modify it, and an attempted write should produce a compiler diagnostic. This restriction applies to the applicable access, not necessarily to every view of the same object; another part of the program may access it without the qualification and change its value. section 5.6.1

An array referring to string literals

For an array such as bird, neither the array entries nor the string data they refer to may be modified through the shown const-qualified types. The array stores references to string literals elsewhere; it does not contain the literal characters themselves. section 5.6.1

String literals are also read-only, although their historical C type is not const-qualified. A third category, temporary objects, is identified but deferred for later treatment. section 5.6.1 section 13.2.2

Visualizes how bird refers to string literals stored elsewhere. section 5.6.1

How Modern C covers it

How deep it goes
Defines const-qualified objects and string literals, explains qualification limits, and defers temporary objects and pointer mechanics.
Examples
Uses the bird array, compiler diagnostics, and string literals referenced from storage outside the array.
What you can do after
Evaluate which C writes a const-qualified access permits and recognize why string literals must be treated as read-only.
How it connects
Connects read-only objects with const qualification, string literals, arrays, pointer types, and temporary objects.

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.