This hack will do it. In practice, sizeof(void *) is equal to sizeof(size_t) on each but the most obscure platform. However, I would advise against using this. Instead, -Weverything should be discarded. The standard C functions date back to 70 ', and the original C compilers were much less strict than today Clang or GCC with warnings enabled. The fact that you will find something βunsafeβ in some of them is inevitable.
void * memchr_(const void * ptr_, int c, size_t num); int main(void) { unsigned char ary[] = { 1, 6, 2, 45, 23, 75, 23, 43, 23 }, * ptr = NULL; ptr = memchr_(ary, 23, sizeof(ary) / sizeof(ary[0])); printf("ary = %p, ptr = %p, *ptr = %u\n", (void *)ary, (void *)ptr, *ptr); return 0; } void * memchr_(const void * ptr_, int c, size_t num) { size_t i; const unsigned char * ptr = ptr_; for(i = 0; i < num; i++) { if(ptr[i] == (unsigned char)c) { return (unsigned char *)(size_t)ptr + i; } } return NULL; }
Piotr Olszewski
source share