ft_memchr.c - chanhl22/libft GitHub Wiki

  1 /* ************************************************************************** */
  2 /*                                                                            */
  3 /*                                                        :::      ::::::::   */
  4 /*   ft_memchr.c                                        :+:      :+:    :+:   */
  5 /*                                                    +:+ +:+         +:+     */
  6 /*   By: chanhlee <[email protected].>       +#+  +:+       +#+        */
  7 /*                                                +#+#+#+#+#+   +#+           */
  8 /*   Created: 2021/01/16 17:12:08 by chanhlee          #+#    #+#             */
  9 /*   Updated: 2021/02/01 17:55:20 by chanhlee         ###   ########.fr       */
 10 /*                                                                            */
 11 /* ************************************************************************** */
 12 
 13 #include "libft.h"
 14 
 15 void    *ft_memchr(const void *ptr, int value, size_t n)
 16 {
 17     unsigned char   *ptr_str;
 18     size_t          i;
 19     unsigned char   c;
 20 
 21     ptr_str = (unsigned char*)ptr;
 22     c = (unsigned char)value; ////ํ•จ์ˆ˜ ๋‚ด๋ถ€์ ์œผ๋กœ ํ•œ ๋ฐ”์ดํŠธ์”ฉ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ unsigned char๋กœ ๋ณ€ํ™˜ํ•ด ์‚ฌ์šฉ
 23     i = 0;
 24     while (i < n)                                                                         
 25     {
 26         if (ptr_str[i] == c) //ํ•จ์ˆ˜ ๋‚ด๋ถ€์ ์œผ๋กœ ํ•œ ๋ฐ”์ดํŠธ์”ฉ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ unsigned char๋กœ ๋ณ€ํ™˜ํ•ด ์‚ฌ์šฉ
 27             return ((void*)ptr_str + i); //๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋ก์—์„œ value ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๊ณณ์˜ ์ฃผ์†Œ๋ฅผ ๋ฆฌํ„ด
 28         i++;
 29     }
 30     return (NULL);
 31 }