cast discards qualifiers from pointer target type?
Solution 1
You have declared returnvalue as a pointer to a const char, but then you've cast it to a pointer to non-const char. You've discarded the const qualifier, so the compiler complains that you've discarded it!
The solution is either to change the return type of the function, or to find a non-const char to point at. You don't have one in your function, so you could consider changing the argument type if you really need a non-const return type.
Solution 2
You are casting a const char * (let's call it non-modifiable string) to a char * (modifiable string) you discard the const qualifier.
Nigel Ridley
Updated on May 08, 2020Comments
-
Nigel Ridley over 2 years
-Wcast-qualoutputs this warning on stristr()'sreturnline. What is the problem ?warning: cast discards qualifiers from pointer target type
char *stristr(const char *string, const char *substring) { size_t stringlength = strlen(string); char *stringlowered = malloc(stringlength + 1); strcpy(stringlowered, string); tolower2(stringlowered); // in my source it has a different name, sorry. char *substringlowered = malloc(strlen(substring) + 1); strcpy(substringlowered, substring); tolower2(substringlowered); // in my source it has a different name, sorry. const char *returnvalue = strstr(stringlowered, substringlowered); if(returnvalue != NULL) { size_t returnvaluelength = strlen(returnvalue); returnvalue = string; returnvalue += stringlength - returnvaluelength; } free(stringlowered); free(substringlowered); return (char *)returnvalue; }EDIT :
In glibc 2.15's strstr() source code:
return (char *) haystack_start; // cast to (char *) from const char * -
MByD over 10 yearsyou should probably change the return value toconst char *and remove the casting. -
Nigel Ridley over 10 yearsThe prototype is exactly the same as standard C's strstr() source code which throws tons of
discards qualifierswarnings. I guess the only solutions is to makestringnotconst. -
Nigel Ridley over 10 yearsThe prototype is exactly the same as standard C's strstr().
-
Michael Burr over 10 years@Nigel: standard C'sstrstr()will do the same cast away ofconstthat you do - it's just that the library doesn't get compiled during your build, so the warning there is long gone (actually I'm sure it's not even enabled for the build of the library). You may need to disable this particular warning when building yourstristr()function if you want it to have the same property that the standard library has of returning a non-const pointer into a string that had a const-pointer (and therefore placing the responsibility on the user to avoid doing the wrong thing with the pointer). -
Oliver Charlesworth over 10 years@NigelRidley: Well, there's no one standard C implementation, but I imagine the core of most C libraries were implemented well before language standardisation had occurred. Hence the questionable code if you look to deep! It's also worth bearing in mind that the libraries are usually written by the compiler authors, so they're free to make all sorts of kludges that would otherwise be questionable/implementation-defined/undefined.