Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Philipp Klaus Krause <pkk@spth.de> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Making the element type of string literals const |
| Date | 2020-08-14 11:34 +0200 |
| Organization | solani.org |
| Message-ID | <rh5lrt$m1k$1@solani.org> (permalink) |
C does not allow programs to modify a string literal.
However, for historic reasons this is not reflected in their type.
E.g. this compiled, but has undefined behaviour if ever executed:
void f(void)
{
"test"[2] = 'a';
}
It would be more consistent is the element type for string literals
would be const-qualified. And coding standards (e.g. MISRA) require the
use of const-qualified types already in e.g.
const char *c = "test"; // OK
vs.
char *c = "test"; // Allowed by C standard, not allowed by MISRA
I don't think this change would break many recent programs. So even if
the type of char string literals cannot be changed, such a change should
at least be possible for string literals of other types.
Is it worth writing a proposal for C2X?
What do you think of such a change?
Philipp
Back to comp.std.c | Previous | Next — Next in thread | Find similar | Unroll thread
Making the element type of string literals const Philipp Klaus Krause <pkk@spth.de> - 2020-08-14 11:34 +0200
Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-14 13:43 +0200
Re: Making the element type of string literals const Florian Weimer <fw@deneb.enyo.de> - 2020-08-14 23:41 +0200
Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-17 15:23 +0200
csiph-web