I use strlen()
call all over my project, until now I compiled my project without -Wall
compiler option. But when I start using -Wall
I face so many compiler warning. 80% are the strlen char *
vs const char *
warning.
I am aware of type casting all strlen()
calls. Is there any other way that I can suppress the following warning?
./Proj.c:3126: warning: pointer targets in passing argument 1 of
'strlen' differ in signedness`
C:/staging/usr/include/string.h:397: note: expected 'const char *' but
argument is of type 'unsigned char *'`
strlen
takes a const char*
as its input.
Unfortunately the C standard states that signedness of char
is down to the compiler and platform. Many programmers therefore opt to set the signedness of char
explicitly using signed char
or unsigned char
.
But doing that will cause warnings to be emitted if char*
has the other sign convention to what you expect.
Luckily in the context of strlen
, taking a C-style cast is safe: use strlen((const char*)...);