c - Pass char* to method expecting unsigned char* -
i working on embedded device has sdk. has method like:
messagebox(u8*, u8*); // u8 typedefed unsigned char when checked
but have seen in examples calling code like:
messagebox("hi","hello");
passing char pointer without cast. can well defined? asking because ran tool on code, , complaining above mismatch:
messagebox("status", "error calculating \rhash"); diy.c 89 error 64: type mismatch (arg. no. 1) (ptrs signed/unsigned) diy.c 89 error 64: type mismatch (arg. no. 2) (ptrs signed/unsigned)
different opinions on answer , confuses me more. sum up, using api way described above, problem? crash program?
and nice hear correct way pass string sdk methods expecting unsigned char*
without causing constraint violation?
it constraint violation, technically not defined, in practice, not problem. yet should cast these arguments silence these warnings. alternative littering code ugly casts define inline function:
static inline unsigned char *ucstr(const char *str) { return (unsigned char *)str; }
and use function wherever need pass strings apis (mistakenly) take unsigned char *
arguments:
messagebox(ucstr("hi"), ucstr("hello"));
this way not warnings while keeping type safety.
also note messagebox
should take const char *
arguments. sdk uses questionable conventions.
Comments
Post a Comment