String searching for u_char*
How do i look for a substr in string pointed by u_char* as all C functions take a char* as argum开发者_运维问答ent?
Assuming that u_char
is a typedef for unsigned char
, then the normal trick is to use the standard functions and apply casts.
That is very ungainly, though, so you might instead use macros that apply the casts automatically.
#define STRSTR(haystack, needle) \
((u_char *)strstr((char *)haystack, (char *)needle))
Or you might write inline
functions if your compiler supports C99.
static inline u_char *ustrstr(u_char *haystack, u_char *needle)
{
return (u_char *)strstr((char *)haystack, (char *)needle);
}
Or you might simply write cover functions:
u_char *ustrstr(u_char *haystack, u_char *needle)
{
return (u_char *)strstr((char *)haystack, (char *)needle);
}
Since u_char is defined as a typedef of unsigned char in BSD type sockets implementations (sys/types.h in *nix and winsock.h in Windows) and if that's your context, you'll be safe to apply a cast to (char*)
just before passing them to the standard C functions. If that's not your context, then you have to find the declaration of u_char and confirm that it is of type unsigned char or equivalent and only then it's safe to apply the cast.
精彩评论