...
EXP05-EX2: A number of C standard library functions are specified to return non-const pointers that refer to their const-qualified arguments. When the actual arguments to such functions reference const objects, attempting to use the returned non-const pointers to modify the const objects would be a violation of EXP40-C. Do not modify constant valuesobjects and would lead to undefined behavior. These functions are the following:
...
| Code Block |
|---|
extern const char s[];
char* where;
where = strchr(s, '\0');
/* Modifying *s is undefined. */
|
Similarly, in the following example, the function strtol sets the unqualified char* pointer referenced by end to point just past the last successfully parsed character of the constant character array s (which could be stored in ROM). Even though the pointer is not const, attempting to modify the character it points to would lead to undefined behavior.
| Code Block |
|---|
extern const char s[];
long x;
char* end;
x = strtol(s, &end, 0);
/* Modifying **end is undefined. */
|
EXP05-EX3: Because const means "read-only," and not "constant," it is sometimes useful to declare struct members as (pointer to) const objects to obtain diagnostics when the user tries to change them in some way other than via the functions that are specifically designed to maintain that data type. Within those functions, however, it may be necessary to strip off the const qualification to update those members.
...