Answer the question
In order to leave comments, you need to log in
Why can you change a constant character array in C?
Why is this allowed in C?
const char string[] = "Hello world";
char * ptr = strchr(string, 'H');
puts(string); // Hello world
// string[0] = 'h'; // error
*ptr = 'h';
puts(string); // hello world
Answer the question
In order to leave comments, you need to log in
Why is this allowed in C?
If an attempt is made to modify an object defined with a const-qualified type through use
of an lvalue with non-const-qualified type, the behavior is undefined.
const char *
but return char *
? To show that she herself does not change her argument, but not to force all her users to cast the result. I would say that this function is the problem, but it is so old that no one will fix its prototype. char *strchr(char *, char)
and const char *strchr(const char *, char)
, but there is no function overloading in C.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question