ft_memchr
Subject
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.
Understandable explanation
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.
Hints
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
* unsigned char
*/
/* loop over the byte string until our counter is equal to n */
/* compare the current byte to c */
/* if they are the same, return the address of this byte as a
* void *
*/
/* if we searched n bytes and didn't find what we were looking for
* return NULL
*/
/* as you can see, this is very close to the strchr and strrchr
* functions, so take a look at these before looking at the
* solution
*/
}
Commented solution
Last updated
Was this helpful?