From c45ca641c40bf3d5d798b2b6242dc65967eb5012 Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Sun, 8 Apr 2018 13:43:03 +0200 Subject: [PATCH] ft_memcmp: add len 0 handling --- ft_memcmp.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ft_memcmp.c b/ft_memcmp.c index 70a52ba..8e8b9d6 100644 --- a/ft_memcmp.c +++ b/ft_memcmp.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/06 16:55:25 by tmaze #+# #+# */ -/* Updated: 2018/04/06 17:27:15 by tmaze ### ########.fr */ +/* Updated: 2018/04/08 13:41:54 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,18 @@ int ft_memcmp(const void *s1, const void *s2, size_t n) { - size_t i; - int diff; - unsigned char *str1; - unsigned char *str2; + size_t i; + int diff; + void *str1; + void *str2; i = 0; - str1 = (unsigned char*)s1; - str2 = (unsigned char*)s2; - diff = str1[0] - str2[0]; + diff = 0; + str1 = (void*)s1; + str2 = (void*)s2; + if (n > 0) + diff = ((unsigned char*)str1)[0] - ((unsigned char*)str2)[0]; while (diff == 0 && ++i < n) - diff = str1[i] - str2[i]; + diff = ((unsigned char*)str1)[i] - ((unsigned char*)str2)[i]; return (diff); }