-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memcmp.c
46 lines (40 loc) · 1.84 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
36
37
38
39
40
41
42
43
44
45
46
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_memcmp.c :+: :+: */
/* +:+ */
/* By: cwesseli <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/10/10 09:27:09 by cwesseli #+# #+# */
/* Updated: 2022/10/31 10:25:56 by cwesseli ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *str1;
const unsigned char *str2;
size_t i;
str1 = (const unsigned char *) s1;
str2 = (const unsigned char *) s2;
i = 0;
while (i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}
// DESCRIPTION
// The memcmp() function compares the first n bytes (each
// interpreted as unsigned char) of the memory areas s1 and s2.
// RETURN VALUE
// The memcmp() function returns an integer less than, equal to, or
// greater than zero if the first n bytes of s1 is found,
// respectively, to be less than, to match, or be greater than the
// first n bytes of s2.
// For a nonzero return value, the sign is determined by the sign of
// the difference between the first pair of bytes (interpreted as
// unsigned char) that differ in s1 and s2.
// If n is zero, the return value is zero.