Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: discards 'const' qualifiers from pointer target type

Tags:

c

Doesn't const char *s means that "s is a pointer which is pointing towards a constant char " then why it is giving me this warning? I am not trying to change values.

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

and in second warning is assignment discards 'const' qualifiers from pointer target type.

I was trying to make library functions which are defined in string.h, and also tell me how to correct 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;
}
like image 614
Ummair Avatar asked May 11 '16 11:05

Ummair


People also ask

Is it possible to assign a const pointer to a char*?

For second warning: assignment discards 'const' qualifiers from pointer target type. 's2ptr' is of type char * and 's2' is of type const char *. You are not allowed to assign a const char* value to a char * pointer. And regarding how to fix this warning... It depends on what you are trying to do.

Why do I get a pointer-to-type warning when converting a char to pointer?

You get the warning because you are trying to convert this into a pointer pointing to a (non-constant) char. There is a rule in C saying that it is always ok to convert from pointer-to-type into pointer-to-const-type, but not the other way around.

What is the difference between first function warning and assignment warning?

In first function warning is return discards 'const' qualifiers from pointer target type. and in second warning is assignment discards 'const' qualifiers from pointer target type. I was trying to make library functions which are defined in string.h, and also tell me how to correct it.

Is it OK to convert from pointer-to-type to pointer-to const-type?

There is a rule in C saying that it is always ok to convert from pointer-to-type into pointer-to-const-type, but not the other way around. It doesn't matter if your code tries to change the values or not.


2 Answers

Doesn't const char *s means that "s is a pointer which is pointing towards a constant char"

Indeed it does. You get the warning because you are trying to convert this into a pointer pointing to a (non-constant) char. There is a rule in C saying that it is always ok to convert from pointer-to-type into pointer-to-const-type, but not the other way around.

It doesn't matter if your code tries to change the values or not. Just by using char* you tell the compiler that you want a pointer which is allowed to change values.

Most of the C standard library functions do not always make sense when it comes to "const correctness". There is for example no way to cleanly implement strchr. You will have to return (char*)s and cast away the const, which is very bad programming practice. This is the fault of the person who specified the strchr function: it is flawed by design.

like image 162
Lundin Avatar answered Oct 26 '22 18:10

Lundin


For first warning: return discards 'const' qualifiers from pointer target type

C does not have an implicit conversion from const-qualified pointer types to non-const-qualified ones, so to overcome the warning you need to add it explicitly.

Replace return s; with return (char *)s;

For second warning: assignment discards 'const' qualifiers from pointer target type

  • 's2ptr' is of type char * and 's2' is of type const char *
  • You are not allowed to assign a const char* value to a char * pointer.

And regarding how to fix this warning... It depends on what you are trying to do. Either you can make char *s2ptr as const char * s2ptr or remove the const from s2.

So if you wish to convert char *s2ptr to const char *s2ptr, do remember to explicitly cast s2ptr to (char *)s2ptr while returning it in the my_strpbrk() function.

like image 28
Liju Thomas Avatar answered Oct 26 '22 16:10

Liju Thomas