Sujet : Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 02. Aug 2024, 19:24:06
Autres entêtes
Organisation : None to speak of
Message-ID : <87v80ig4vt.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6
User-Agent : Gnus/5.13 (Gnus v5.13)
Richard Harnden <
richard.nospam@gmail.invalid> writes:
[...]
Is there any reason not to always write ...
>
static const char *s = "hello, world";
>
... ?
>
You get all the warnings for free that way.
The "static", if this is at block scope, specifies that the pointer
object, not the array object, has static storage duration. If it's at
file scope it specifies that the name "s" is not visible to other
translation units. Either way, use it if that's what you want, don't
use it if it isn't.
There's no good reason not to use "const". (If string literal objects
were const, you'd have to use "const" here.)
If you also want the pointer to be const, you can write:
const char *const s = "hello, world";
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */