I am little confused about the working technique of memchr() in C . I have observed different implementation of memchr() and found that first it takes and converts the character or number to unsigned char type and then searches a array byte by byte.
I have two questions:
1.If it converts the anything to unsigned char then how it compares a number which has size bigger than unsigned char, for example int type.
2.If it compares byte by byte and returns then returns the address of the first occurence of the character, then suppose I want to search 0x8 in a array.
#include <stdio.h>
#include <string.h>
int main(void)
{
const int arr[5] = {0x1021, 0x8988, 0x706, 0x50, 0x22};
int * ptr;
ptr = memchr(arr, 0x89, sizeof(arr));
printf("arr:%p ptr:%p\n", arr, ptr);
return 0;
}
It should return the address of the 3rd byte of the array as the 0x89 matches with first byte of the element 0x8988 of the array which is at 7th byte as memchr() matches byte by byte (unsigned char) not int type.
Assuming: int is 4 bytes and unsigned char is 1 byte.