✅memchr
- The memchr() function shall locate the first occurrence of
c
(converted to an unsigned char
) in the initial n
bytes (each interpreted as unsigned char
) of the object pointed to by s
.
s
에서 n
바이트 중에서 c
를 찾는 함수. 즉, 메모리에서 특정 값을 찾을 때 사용하는 함수.
💾함수 형태
void *memchr(const void *s, int c, size_t n);
💻Parameters
s
: 검색을 수행하는 시작 주소
c
: 검색 문자
n
: 감색을 시작한 부분부터 검색을 수행할 만큼의 바이트 수
💻Return value
- 처음 발견된 위치의 포인터. 발견하지 못하면 NULL
💾함수 구현
#include "libft.h"
void* ft_memchr(const void* s, int c, size_t n)
{
unsigned char* s1;
size_t i;
s1 = (unsigned char*)s;
i = 0;
while (i < n)
{
if (s1[i] == (unsigned char)c)
return ((void *)&s1[i]);
i++;
}
return (0);
}
💾사용 예시
#include <stdio.h>
int main(void)
{
char str[] = "www.42seoul.kr";
printf("%s\n", ft_memchr(str, 'k', 12));
printf("%s\n", ft_memchr(str, 'k', 14));
return 0;
}