ft_memchr

jaehlee·2025년 4월 25일

Libft

목록 보기
5/26

1. ft_memchr이란?


*buf주소부터 count바이트만큼 검색해서 c를 찾는함수

2. 함수 프로토타입

void	*ft_memchr(const void *buf, int c, size_t count)

3. 함수구현

#include "libft.h"

void	*ft_memchr(const void *buf, int c, size_t count)
{
	const unsigned char	*str;
	size_t				i;

	str = (const unsigned char *) buf;
	i = 0;
	while (i < count)
	{
		if (str[i] == (unsigned char)c)
			return ((void *)&str[i]);
		i++;
	}
	return (0);
}

사용 예시

int main(void)
{
    const char data[] = {0x10, 0x20, 0x30, 0x40, 0x50};
    unsigned char target = 0x30;
    size_t size = sizeof(data);

    unsigned char *result = ft_memchr(data, target, size);
    if (result != NULL)
        printf("0x%x found at index %ld\n", target, result - (unsigned char *)data);
    else
        printf("0x%x not found\n", target);
    return 0;
}
profile
공부하는 개발자

0개의 댓글