Deutsch   English   Français   Italiano  
<87frr7g3vq.fsf@nosuchdomain.example.com>

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: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: relearning C: why does an in-place change to a char* segfault?
Date: Tue, 13 Aug 2024 20:49:45 -0700
Organization: None to speak of
Lines: 24
Message-ID: <87frr7g3vq.fsf@nosuchdomain.example.com>
References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk>
	<v8fhhl$232oi$1@dont-email.me> <v8fn2u$243nb$1@dont-email.me>
	<87jzh0gdru.fsf@nosuchdomain.example.com> <865xs54fak.fsf@linuxsc.com>
	<v9fqtb$3t7ph$4@dont-email.me> <86h6boszrb.fsf@linuxsc.com>
	<87jzgjg9k3.fsf@nosuchdomain.example.com>
	<20240813200959.565@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Wed, 14 Aug 2024 05:49:46 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8fd33d9227c54bb33f07e63da240e3d2";
	logging-data="322464"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19VCsqkDyIvkjgbKnus7AL0"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:+O6vcegQNRK3heafuPZpR5sJG9s=
	sha1:m8mJDilT1oN71rMjSxD4kvlaL2Q=
Bytes: 2298

Kaz Kylheku <643-408-1753@kylheku.com> writes:
[...]
> Also, <string.h> could have type generic functions where it
> makes sense to support both const char * and char *.
>
> E.g. strchr should could return const char * if the
> parameter is const char *, and char * when the parameter is char *.
> The one function we have now strips the qualifier, which is bad;
> when you find a character in a const string, you get a non-const
> pointer to it.

C23 does exactly this.  It changes memchr, strchr, strpbrk, strrchr, and
strstr into generic functions (macros, presumably using _Generic) whose
return type is pointer-to-const if and only if the appropriate argument
is pointer-to-const.  If you suppress the macro definition, you get a
function that takes a const-qualified argument and returns a non-const
result.

(C++ does something similar for its functions in <cstring>, imported
from C, but by making them templates.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */