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;
}
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.
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.
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.
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.
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.
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
char *
and 's2' is of type const char *
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With