user2979872 user2979872 - 1 year ago 125
C Question

warning when strlen is used on unsigned char array and signed char array

I don't understand why the last two lines cause warning using strlen(). Shouldn't the compiler ignore these?

size_t len;
char cstr[] = "char string";
signed char scstr[] = "signed char string";
unsigned char ucstr[] = "unsigned char string";

len = strlen(cstr);
len = strlen(scstr); /* warns when char is unsigned */
len = strlen(ucstr); /* warns when char is signed */

Answer Source

Because the prototype is:

size_t strlen ( const char * str );

as stated on the ref.

An implicit conversion is happening in these lines, thus the warnings. Read more here: How can I avoid gcc warning for plain "char" to : "unsigned char" OR "signed char" conversion?

As Pete Becker stated:

"char to unsigned char involves an implicit conversion. unsigned char* to char*, as in the code in the question, is not valid, and requires a diagnostic. "

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download