From 6236107bd48c008cc4ac9ef209264977d65ea9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 23 Oct 2024 10:35:01 +0200 Subject: [PATCH] fix(ft_strchr): c was not cast to char for check agains \0 --- ft_strchr.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ft_strchr.c b/ft_strchr.c index 4eb85f4..b448533 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/16 10:34:23 by kcolin #+# #+# */ -/* Updated: 2024/10/18 17:42:51 by kcolin ### ########.fr */ +/* Updated: 2024/10/23 10:33:55 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,29 +17,35 @@ char *ft_strchr(const char *s, int c) i = 0; while (s[i] != '\0') { - if (s[i] == (unsigned char)c) + if (s[i] == (char)c) return ((char *)s + i); i++; } - if (c == '\0') + if ((char)c == '\0') return ((char *)s + i); return (0); } /* -#include +#include // bad +#include // bad int main(int argc, char **argv) { char *result; - if (argc > 2) + if (argc > 1) { - result = ft_strchr(argv[1], argv[2][0]); + result = ft_strchr(argv[1], '\0' + 256); if (result == 0) printf("(null)\n"); else - printf("%s\n", result); + printf("[%p]\n", result); + result = strchr(argv[1], '\0' + 256); + if (result == 0) + printf("(null)\n"); + else + printf("[%p]\n", result); } return (0); }