Warning: discards "const" from the target pointer type

Does it const char *smean that "s is a pointer pointing to a constant char", then why does it give me this warning? I am not trying to change the values.

In the first warning function return discards 'const' qualifiers from pointer target type.

and in the second - assignment discards 'const' qualifiers from pointer target type.

I tried to create library functions that are defined in string.h, and also tell me how to fix it.

char *my_strchr( const char *s, int c )
{
    for(;*s!='\0';s++)
       if(*s==c)
          return s; // warning

    return 0;
}



char *my_strpbrk( const char *s1, const char *s2 )
{
    char *s2ptr;

    for(;*s1!='\0';s1++)
        for(s2ptr=s2;*s2ptr!='\0';s2ptr++) //warning
           if(*s1==*s2ptr)
               return s2ptr;

    return 0;
}
+4
source share
3 answers

Not const char * s means that "s is a pointer that points to a constant char"

, . , , () char. C , , --const-, .

, . char*, , , .

C , "const correctness". , strchr. (char*)s const, . , strchr: .

+7

: 'const'

C const-qualified -, .

return s; return (char *)s;

: 'const'

  • 's2ptr' char * 's2' const char *
  • const char* char *.

, ... , . char *s2ptr const char * s2ptr, const s2.

, char *s2ptr const char *s2ptr, s2ptr (char *)s2ptr, my_strpbrk().

+1

- char. const char .

s2ptr=s2. s2ptr char, s2 const char.

-1

All Articles