Sujet : Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
De : dave_thompson_2 (at) *nospam* comcast.net
Groupes : comp.lang.cDate : 25. Aug 2024, 21:52:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <ka6ncjp0ca2dvf6v6lbg6faindvgmujtoa@4ax.com>
References : 1 2 3 4 5 6
User-Agent : Forte Agent 3.3/32.846
On Fri, 2 Aug 2024 13:04:55 +0100, Richard Harnden
<
richard.nospam@gmail.invalid> wrote:
[string literals not typed const in C even though writing prohibited]
Is there any reason not to always write ...
static const char *s = "hello, world";
... ?
You get all the warnings for free that way.
But sizeof s is 8 or 4 regardless of the string, while sizeof "some
string" is the length of the string plus 1 (for the null terminator).
static const char s[] = "hello, world";
// autosized by initializer
would be a better replacement, or in C99+ if at file scope
(const char[]){"hello, world"}