ft_memcmp: add len 0 handling

This commit is contained in:
Tanguy MAZE 2018-04-08 13:43:03 +02:00
parent 2608cfbc5a
commit c45ca641c4

View File

@ -6,7 +6,7 @@
/* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */ /* By: tmaze <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/06 16:55:25 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,14 +16,16 @@ int ft_memcmp(const void *s1, const void *s2, size_t n)
{ {
size_t i; size_t i;
int diff; int diff;
unsigned char *str1; void *str1;
unsigned char *str2; void *str2;
i = 0; i = 0;
str1 = (unsigned char*)s1; diff = 0;
str2 = (unsigned char*)s2; str1 = (void*)s1;
diff = str1[0] - str2[0]; str2 = (void*)s2;
if (n > 0)
diff = ((unsigned char*)str1)[0] - ((unsigned char*)str2)[0];
while (diff == 0 && ++i < n) while (diff == 0 && ++i < n)
diff = str1[i] - str2[i]; diff = ((unsigned char*)str1)[i] - ((unsigned char*)str2)[i];
return (diff); return (diff);
} }