-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memcmp.c
35 lines (33 loc) · 1.51 KB
/
ft_memcmp.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oostapen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 00:12:52 by oostapen #+# #+# */
/* Updated: 2024/04/15 13:34:12 by oostapen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* `ft_memcmp` - "memory compare", compares `n` bytes of `s1` and `s2`.
*
* Key points:
* 1. Takes `s1`, `s2`, `n`.
* 2. Casts `s1`, `s2` to `unsigned char` for byte comparison.
* 3. Uses counter `i`.
* 4. Loops while `i` is less than `n` and bytes are equal.
* 5. Returns 0 if `n` bytes are equal.
* 6. Otherwise, returns the difference of the first unequal bytes.
*/
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while ((i < n) && (((unsigned char *)s1)[i] == ((unsigned char *)s2)[i]))
i++;
if (i == n)
return (0);
return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i));
}