Why doesn't strerror return a const-qualified pointer?

651 Views Asked by At

I was just skimming the C99 standard, looking for something that I don't remember now, when I noticed that the pointer returned from the strerror() function (section 7.12.6.2) isn't const-qualified, even though the standard says:

The strerror function returns a pointer to the string, the contents of which are
locale-specific. The array pointed to shall not be modified by the program,
but may be overwritten by a subsequent call to the strerror function.

Is there an obvious reason for this function to return a modifiable string instead of something like:

char const * const strerror(int errnum);  

or at the very least

char const * strerror(int errnum);
3

There are 3 best solutions below

2
On BEST ANSWER

Same as for the type of string literals: It was already like that in C89, describing a practice dating back to before the introduction of const in the language. Changing it would make current valid program invalid.

2
On

Response about static buffer is wrong; whether the pointer type returned is const or not has nothing to do with the buffer. The return type is completely about API compatibility with historic code which does not use const, and there's no harm in it. Someone writing modern const-aware code will simply use the return value immediately or store it into a pointer-to-const variable.

0
On

This is probably so because many historical implementations use a static buffer into which they "print" the error string.