Deutsch English Français Italiano |
<20240801174026.00002cda@yahoo.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Michael S <already5chosen@yahoo.com> Newsgroups: comp.lang.c Subject: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault? Date: Thu, 1 Aug 2024 17:40:26 +0300 Organization: A noiseless patient Spider Lines: 42 Message-ID: <20240801174026.00002cda@yahoo.com> References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Thu, 01 Aug 2024 16:39:55 +0200 (CEST) Injection-Info: dont-email.me; posting-host="58102f3bca2f74c91d2f4f49c88a1b8b"; logging-data="2320781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0SICJ0pBDyZQUbljJNyUzhoqPfWDkKFg=" Cancel-Lock: sha1:mx/KO8hvlFSR3EIQjgBIXe1EtiQ= X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32) Bytes: 2306 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*'.