...
This non-compliant code example may pass illegal values to the ctype
functions.
Code Block | ||
---|---|---|
| ||
size_t count_whitespace(const char *s) { const char *t = s; while(isspace(*t)) /* possibly *t < 0 */ ++t; return t - s; } |
...
Pass character strings around explicitly using unsigned characters.
Code Block | ||
---|---|---|
| ||
size_t count_whitespace(const unsigned *s) { const unsigned char *t = s; while(isspace(*t)) ++t; return t - s; } |
...
This compliant solution uses an explicit cast.
Code Block | ||
---|---|---|
| ||
size_t count_whitespace(const char *s) { const char *t = s; while(isspace((unsigned char)*t)) ++t; return t - s; } |
...