Deutsch   English   Français   Italiano  
<v8fhhl$232oi$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!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: relearning C: why does an in-place change to a char* segfault?
Date: Thu, 1 Aug 2024 09:38:13 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <v8fhhl$232oi$1@dont-email.me>
References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk>
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: Thu, 01 Aug 2024 10:38:14 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="217242ba9a802a5b5a0ba9b553f9cfe5";
	logging-data="2198290"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+ZXAtKbMae+gyRlg253KnSyieJPZlKW/Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:dP73ev6HwGgbZL6CfLXTns7ARS0=
In-Reply-To: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk>
Content-Language: en-US
Bytes: 1780

On 01/08/2024 09:06, Mark Summerfield 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);
> }

text is pointing to "this is a test" - and that is stored in the program 
binary and that's why can't modify it.

Change it to:

char text[] = "this is a test";

You can modify that, text gets it's own copy.