-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_memcmp.c
63 lines (58 loc) · 2.23 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jopedro3 <jopedro3@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/04 12:00:11 by jopedro3 #+# #+# */
/* Updated: 2023/10/04 14:47:24 by jopedro3 ### ########.fr */
/* */
/* ************************************************************************** */
/* Function: ft_memcmp
Purpose: Compares two blocks of memory and determines if they are equal
or if one is lexicographically greater than the other.
How it works:
- Casts the input pointers to
unsigned char pointers for byte-wise comparison.
- Initializes a counter 'len' to keep track of checked bytes.
- Uses a while loop to compare each byte of the two memory blocks.
- If a mismatch is found, returns the difference between the two bytes.
- If no mismatch is found after checking 'n' bytes, returns 0.
*/
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t len;
unsigned char *ptr1;
unsigned char *ptr2;
ptr1 = (unsigned char *)s1;
ptr2 = (unsigned char *)s2;
len = 0;
while (len < n)
{
if (ptr1[len] != ptr2[len])
return (ptr1[len] - ptr2[len]);
len++;
}
return (0);
}
/*#include <stdio.h>
int main(void)
{
char str1[15] = "Hello, World!";
char str2[15] = "Hello, World!";
char str3[15] = "Hello, Wxrld!";
int cpy;
cpy = ft_memcmp(str1, str2, 10);
if (cpy == 0)
printf("str1 and str2 are equal in the first 10 bytes\n");
else
printf("str1 and str2 are NOT equal in the first 10 bytes\n");
cpy = ft_memcmp(str1, str3, 10);
if (cpy == 0)
printf("str1 and str3 are equal in the first 10 bytes\n");
else
printf("str1 and str3 are NOT equal in the first 10 bytes\n");
return 0;
}*/