ft_memchr
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
MEMCHR(3) (simplified)
NAME
memchr -- locate byte in byte string
SYNOPSIS
void *memchr(const void *s, int c, size_t n);
DESCRIPTION
the memchr() function locates the first occurence of c (convered to an unsigned char) in string s.
RETURN VALUES
The memchr() function returns a pointer to the byte located, or NULL if no such byte exists within n bytes.
The memchr()
function works similarly as the strchr()
function, the difference is that memchr()
works with byte string (void *
) where strchr()
works with 'litteral' strings (char *
).
This means we can send whatever type of data we want to memchr()
and it'll still work.
memchr()
also has a third parameter, n
. This parameter tells the function how many bytes we want to search in. We need this parameter since s
is not a 'litteral' string, it doesn't have a NUL-terminating character. If we didn't have this parameter, we would be reading a random number of bytes each time.
void *ft_memchr(const void *s, int c, size_t n)
{
/* as said in the man, the search is done for c converted to
* an unsigned char, so we have to convert both c and s to