Deutsch English Français Italiano |
<slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> 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 05:30:02 -0000 (UTC) Organization: the-candyden-of-code Lines: 74 Message-ID: <slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <20240801174026.00002cda@yahoo.com> <v8gi7i$29iu1$1@dont-email.me> Injection-Date: Fri, 02 Aug 2024 07:30:03 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c019fcf938d6f719ee323756ceea5c25"; logging-data="2813877"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hBe6vyNJ240FqxACpqtUEvrStABQHUy6MGxHLpn5LBA==" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:LiPvxyHUtLtUMPIeSuRXT/qrvdo= X-Face: b{dPmN&%4|lEo,wUO\"KLEOu5N_br(N2Yuc5/qcR5i>9-!^e\.Tw9?/m0}/~:UOM:Zf]% b+ V4R8q|QiU/R8\|G\WpC`-s?=)\fbtNc&=/a3a)r7xbRI]Vl)r<%PTriJ3pGpl_/B6!8pe\btzx `~R! r3.0#lHRE+^Gro0[cjsban'vZ#j7,?I/tHk{s=TFJ:H?~=]`O*~3ZX`qik`b:.gVIc-[$t/e ZrQsWJ >|l^I_[pbsIqwoz.WGA]<D Bytes: 4350 David Brown <david.brown@hesbynett.no> wrote at 17:56 this Thursday (GMT): > On 01/08/2024 16:40, Michael S wrote: >> On Thu, 01 Aug 2024 08:06:57 +0000 >> Mark Summerfield <mark@qtrac.eu> wrote: >> >>> This program segfaults at the commented line: >>> >>> #include <ctype.h> >>> #include <stdio.h> >>> >>> void uppercase_ascii(char *s) { >>> while (*s) { >>> *s = toupper(*s); // SEGFAULT >>> s++; >>> } >>> } >>> >>> int main() { >>> char* text = "this is a test"; >>> printf("before [%s]\n", text); >>> uppercase_ascii(text); >>> printf("after [%s]\n", text); >>> } >>> >> >> The answers to your question are already given above, so I'd talk about >> something else. Sorry about it. >> >> To my surprise, none of the 3 major compilers that I tried issued the >> warning at this line: >> char* text = "this is a test"; >> If implicit conversion of 'const char*' to 'char*' does not warrant >> compiler warning than I don't know what does. >> Is there something in the Standard that explicitly forbids diagnostic >> for this sort of conversion? >> >> BTW, all 3 compilers issue reasonable warnings when I write it slightly >> differently: >> const char* ctext = "this is a test"; >> char* text = ctext; >> >> I am starting to suspect that compilers (and the Standard?) consider >> string literals as being of type 'char*' rather than 'const char*'. >> > > Your suspicions are correct - in C, string literals are used to > initialise an array of char (or wide char, or other appropriate > character type). Perhaps you are thinking of C++, where the type is > "const char" (or other const character type). > > So in C, when a string literal is used in an expression it is converted > to a "char *" pointer. You can, of course, assign that to a "const char > *" pointer. But it does not make sense to have a warning when assigning > it to a non-const "char *" pointer. This is despite it being undefined > behaviour (explicitly stated in the standards) to attempt to write to a > string literal. > > The reason string literals are not const in C is backwards compatibility > - they existed before C had "const", and making string literals into > "const char" arrays would mean that existing code that assigned them to > non-const pointers would then be in error. C++ was able to do the right > thing and make them arrays of const char because it had "const" from the > beginning. > > 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. -- user <candycane> is generated from /dev/urandom