Sujet : Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 02. Aug 2024, 19:58:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8ja83$2us0r$2@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 8/2/24 14:42, Richard Damon wrote:
On 8/2/24 2:24 PM, Keith Thompson wrote:
Richard Harnden <richard.nospam@gmail.invalid> writes:
[...]
Is there any reason not to always write ...
>
static const char *s = "hello, world";
>
... ?
...
There's no good reason not to use "const". (If string literal objects
were const, you'd have to use "const" here.)
...
The one good reason to not make it const is that if you are passing it
to functions that take (non-const) char* parameters that don't
actually change that parameters contents.
Actually, that's not a good reason. If you can't modify the function's
interface, you should use a (char*) cast, which will serve to remind
future programmers that this is a dangerous function call. You shouldn't
make the pointer's own type "char *".