Why most c functions with string in param usually has definition char* and not unsigned char*? According to my understanding unsigned char* is more generic.
And how to deal in situations when I have unsigned char* and need to pass to char* in correct way?
For example is it good style to call function in sample below?
unsigned char* a=...
strlen((char*)a);
The standard states that :
charis guaranteed to be able to represent all characters as a positive number.Moreover :
It is implementation defined if
charis signed or not.The type
charis, thus, the type defined to be the best fit to store characters for the implementation. There are no need to cast it to anunsigned char.Also, you say that "According to my understanding
unsigned char*is more generic.". You may be referring to the fact that the sign ofcharis implementation defined, and usingunsignedmakes it more predictable across implementations. While this would be true when using it for arithmetic, the standard ensures that when using it for string and characters, the value is never negative, making it predictable across all implementations.