Path: ...!feeds.phibee-telecom.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault? Date: Fri, 02 Aug 2024 03:02:03 -0700 Organization: None to speak of Lines: 38 Message-ID: <87zfpvfdk4.fsf@nosuchdomain.example.com> References: <20240801174026.00002cda@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 02 Aug 2024 12:02:04 +0200 (CEST) Injection-Info: dont-email.me; posting-host="ce4d2d563452819094f001112239b02f"; logging-data="2917230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xgXrrvl0DC9zUblHSCXAA" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:J+3AVnobIkPhUmxlN8C1kVMzpyg= sha1:20mrfvA7XtmyM/kF9VqmSsuRf5k= Bytes: 2551 candycanearter07 writes: > David Brown wrote at 17:56 this Thursday (GMT): [...] >> gcc has the option "-Wwrite-strings" that makes string literals in C >> have "const char" array type, and thus give errors when you try to >> assign to a non-const char * pointer. But the option has to be >> specified explicitly (it is not in -Wall) because it changes the meaning >> of the code and can cause compatibility issues with existing correct code. > > -Wwrite-strings is included in -Wpedantic. No it isn't, nor is it included in -Wall -- and it wouldn't make sense to do so. The -Wpedantic option is intended to produce all required diagnostics for the specified C standard. -Wwrite-strings gives string literals the type `const char[LENGTH]`, which enables useful diagnostics but is *non-conforming*. For example, this program: ``` #include int main(void) { char *s = "hello, world"; puts(s); } ``` is valid (no diagnostic required), since it doesn't actually write to the string literal object, but `-Wwrite-strings` causes gcc to warn about it (because making the pointer non-const creates the potential for an error). -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */