Deutsch English Français Italiano |
<v8ii17$2q5p1$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Richard Harnden <richard.nospam@gmail.invalid> 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, 2 Aug 2024 13:04:55 +0100 Organization: A noiseless patient Spider Lines: 44 Message-ID: <v8ii17$2q5p1$1@dont-email.me> References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <20240801174026.00002cda@yahoo.com> <v8gi7i$29iu1$1@dont-email.me> <slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> <87zfpvfdk4.fsf@nosuchdomain.example.com> Reply-To: nospam.harnden@invalid.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 02 Aug 2024 14:04:55 +0200 (CEST) Injection-Info: dont-email.me; posting-host="1a4dd46279290e01fb60e731a584d26c"; logging-data="2955041"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NMDD5d7cYcKTapZtW2IydPJT/61/ujpk=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:89iJVH/OupeO+2OsXC5a9bVghMU= In-Reply-To: <87zfpvfdk4.fsf@nosuchdomain.example.com> Content-Language: en-US Bytes: 2888 On 02/08/2024 11:02, Keith Thompson wrote: > candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> > writes: >> David Brown <david.brown@hesbynett.no> 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 <stdio.h> > 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). > Is there any reason not to always write ... static const char *s = "hello, world"; .... ? You get all the warnings for free that way.