Do sheets drop qualifiers from the target pointer type?

-Wcast-qualdisplays this warning on the string stristr () return. What is the problem?

warning: cast discards qualifiers from the target pointer 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 strstr () source code:

return (char *) haystack_start; // cast to (char *) from const char *
+5
source share
3 answers

You declared it returnvalueas a pointer to const char, but then you pointed it to a pointer to const char. You have canceled the classifier const, so the compiler complains that you have dropped it!

, -t21, . , , const.

+7

const char * ( ) char * ( ), const.

+5

/

const char *returnvalue = strstr(stringlowered, substringlowered);

char *returnvalue = strstr(stringlowered, substringlowered);

( ).

0

All Articles