Deutsch English Français Italiano |
<IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Thu, 01 Aug 2024 08:06:57 +0000 From: Mark Summerfield <mark@qtrac.eu> Subject: relearning C: why does an in-place change to a char* segfault? Newsgroups: comp.lang.c MIME-Version: 1.0 User-Agent: Pan/0.154 (Izium; 517acf4) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-ID: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> Date: Thu, 01 Aug 2024 08:06:57 +0000 Lines: 40 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-qeYiVctM9WVraOQAFf3mSA3RwHnkeCdAWEWm7Bmnp47igzzKV8qd1ov8/FtDit/tD5TUDi9McKG5HfS!EBs/3ivU2v0n2n5nB2R1xeglMy+7gUC8W3j89rHtxHmCHg5KUukuIX6J8Dr/yKIRSGrAPMkqs/fP!3acCMHs0CXseycU/P9AczgyX6Q== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 1990 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); } I know there are better ways to do ASCII uppercase, I don't care about that; what I don't understand is why I can't do an in-place edit of a non- const char*? I build using scons, which does: gcc -o inplace.o -c -Wall -g inplace.c gcc -o inplace inplace.o The error with gdb is: Starting program: /tmp/inplace/inplace [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". before [this is a test] Program received signal SIGSEGV, Segmentation fault. 0x000055555555516e in uppercase_ascii (s=0x555555556004 "this is a test") at inplace.c:6 6 *s = toupper(*s);