Sujet : Re: relearning C: why does an in-place change to a char* segfault?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 14. Aug 2024, 04:49:45
Autres entêtes
Organisation : None to speak of
Message-ID : <87frr7g3vq.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Gnus/5.13 (Gnus v5.13)
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.comvoid Void(void) { Void(); } /* The recursive call of the void */