-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memchr.c
49 lines (45 loc) · 1.7 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
48
49
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibeliaie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/16 21:59:34 by ibeliaie #+# #+# */
/* Updated: 2023/05/23 13:18:20 by ibeliaie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* locate character in block of memory */
void *ft_memchr(const void *str, int c, size_t len)
{
size_t i;
unsigned char *ptr;
unsigned char uc;
i = 0;
ptr = (unsigned char *)str;
uc = (unsigned char)c;
while (i < len)
{
if (ptr[i] == uc)
return (ptr + i);
i++;
}
return (NULL);
}
// int main()
// {
// char array[60] = "A noisy noise annoys an oyster most.";
// int c = 'i';
// char *ptr;
// unsigned long size = 10;
// ptr = ft_memchr(array, c, size);
// if (ptr != NULL)
// printf("Character '%c' is in position %ld\n", *ptr, (ptr - array));
// else
// printf("Character not found.\n");
// return (0);
// }
//
//treating memory block as an array of unsigned chars, allows for comparison
//of individual bytes regardless of original type of the memory block