Consider an array of labels used only for reading:\n\nchar const*const bird[3] = {\n "raven",\n "magpie",\n "jay",\n};\n\nThe declaration uses const in two places. The array entries cannot be changed, and the strings they refer to cannot be modified through these declarations. A compiler should diagnose attempts to write either one.\n\nTAKEAWAY: An object of const-qualified type is read-only. This is not the same as a constant in C: a const-qualified object may still change if another part of the program accesses it without the qualification. section 5.6 section 5.6.1
Read const-qualified declarations by separating the protected parts:
- const-qualified objects provide read-only access through that type; attempted modifications should produce a diagnostic. section 5.6.1
- String literals are read-only, although their historical C type is not written as an array of const-qualified characters for backward-compatibility reasons. section 5.6.1
- A const-qualified object is not necessarily a compile-time constant; C uses constant in a narrower sense, creating confusion with named constants and null pointer constants. section 5.6 section 11.1.5
- For compound data, const qualification offers read-only access rather than an absolute guarantee that the stored value never changes. section 5.6.1
Two const qualifications in one declaration
In char const*const bird[3], the entries are const-qualified pointers, and each pointer refers to const characters. The array can therefore be traversed for output, but neither its references nor the referenced strings may be modified through bird. Similar declarations support read-only arrays of pronouns and ordinal labels. section 5.6
char const*const bird[3] = {
"raven",
"magpie",
"jay",
};
char const*const pronoun[3] = {
"we",
"you",
"they",
};
char const*const ordinal[3] = {
"first",
"second",
"third",
};
...
for (unsigned i = 0; i < 3; ++i)
printf("Corvid %u is the %s\n", i, bird[i]);
...
for (unsigned i = 0; i < 3; ++i)
printf("%s plural pronoun is %s\n", ordinal[i], pronoun[i]);
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":10}],[{\"line\":5,\"ch\":5},{\"line\":5,\"ch\":10}],[{\"line\":10,\"ch\":5},{\"line\":10,\"ch\":10}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":16,\"ch\":5},{\"line\":16,\"ch\":13}],[{\"line\":19,\"ch\":5},{\"line\":19,\"ch\":13}],[{\"line\":17,\"ch\":4},{\"line\":17,\"ch\":10}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":10}],[{\"line\":16,\"ch\":0},{\"line\":16,\"ch\":3}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":3}],[{\"line\":16,\"ch\":5},{\"line\":16,\"ch\":13}],[{\"line\":19,\"ch\":5},{\"line\":19,\"ch\":13}],[{\"line\":17,\"ch\":4},{\"line\":17,\"ch\":10}],[{\"line\":20,\"ch\":4},{\"line\":20,\"ch\":10}]]"}
!@%STYLE%@!
Inspect the complete listing to see const-qualified pointers used with arrays of strings and read-only traversal. section 5.6






