const-string

Why strrchr() returns `char*` instead of `const char*`?

白昼怎懂夜的黑 提交于 2019-12-07 02:21:53
问题 The function char* strrchr(const char *str, int ch) returns a pointer ( char* ) within str ( const char * ) where the last occurrence of ch is located. So we can write the following code without any cast: #include <string.h> int main() { const char CONSTSTR[] = "foo/bar/foobar.txt"; char *ptr = strrchr (CONSTSTR, '/'); *ptr++ = 'B'; *ptr++ = 'A'; *ptr++ = 'D'; } What is the advantage to return char* instead of const char* ? EDIT: As a Shafik Yaghmour pointed out, there are very good answers