-
Notifications
You must be signed in to change notification settings - Fork 24
/
ft_memchr.c
47 lines (43 loc) · 2.24 KB
/
ft_memchr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbie <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/26 17:23:50 by pbie #+# #+# */
/* Updated: 2015/12/07 22:55:12 by pbie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*According to the man this function locates the first occurence of c (converted
* to an unsigned char) in string s. ft_memchr returns a pointer to the byte
* located, or NULL if no such byte exists within n bytes.*/
void *ft_memchr(const void *s, int c, size_t n)
{
/*We start by creating a char pointer str that we will be placing a casted
* version of our s parameter into. We also create a size_t variable i that
* we will use as a counter. We make it size_t in order to compare it to
* the given size_t n in our loop condition. We i equal to 0 since we will
* also be using it to start at the beginning of our string str. We cast s
* as a char pointer and place it inside our str variable. We then enter
* our loop.*/
char *str;
size_t i;
i = 0;
str = (char *)s;
/*While our i variable is less than the parameter n we will have our loop
* continue. Inside we want to see if the current index position that has
* been casted as an unsigned char is equal to the parameter c that has
* also been casted as an unsigned char. If this statement is true we will
* return a char casted pointer to the index position that contains c. If
* i is no longer less than n and we still have not located c, we will
* return NULL.*/
while (i < n)
{
if ((unsigned char)str[i] == (unsigned char)c)
return ((char *)s + i);
i++;
}
return (NULL);
}