5

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 *'`
3
  • 1
    Why are you using unsigned char pointers for your strings? The fact that they aren't const would not generate a warning by itself. Nov 27, 2014 at 15:36
  • 1
    Force a conversion (char*)expression. Anyway, why is it unsigned char? Nov 27, 2014 at 15:37
  • "Is there any other way that I can suppress the following warning?" Aside from changing code, there certainly must be a compiler option to suppress this one warning type. See stackoverflow.com/questions/13400605/… Nov 27, 2014 at 16:04

3 Answers 3

7

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*)...);

2

There's always the option to do :

inline size_t u_strlen(const unsigned char * array)
{
    return strlen((const char*)array);
}

This way you don`t have to add a conversion everywhere in your code.

Although the question remains, why are you using unsigned char? I suppose it`s a byte array for data packets over networking, in that case you should take care of the length in the protocol anyways.

5
  • Yes, with removing the 'inline' part. size_t is ISO C. Nov 27, 2014 at 15:43
  • 4
    This is not an inline nor size_t issue. What this proposes is function over-loading which is not a C construct. Nov 27, 2014 at 15:45
  • Ah, I am sorry I missed that. You can always rename it to ustrlen or something similar, and just replace the strlen calls to this. Functionality-wise it should still work. Nov 27, 2014 at 15:50
  • Suggest updating your post to reflect renaming the function. BTW inline is just fine in modern C Nov 27, 2014 at 16:02
  • 1
    I suggest const qualifying the argument: size_t u_strlen(const unsigned char *s)
    – chqrlie
    Mar 23, 2021 at 11:43
1

It is not a matter if char* vs const char*, that is not the problem being reported (because it is not a problem). The problem is the fact that you are using unsigned char*. Whether of not a plain char is signed or unsigned is implementation dependent; so on some platforms unsigned char* will be the same as char* and on others it won't.

The best solution is ensure type agreement by not qualifying your strings and string pointers as unsigned; it almost certainly serves no useful purpose. For strings and characters the distinction between signed and unsigned is irrelevant - that is only of interest when performing arithmetic and using char as a "small integer".

Most compilers support a command line switch to specify the default signedness of char; however I would not recommend that as a solution, nor would I recommend casting; correct type agreement should always be your first choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.