先看两个函数的申明:
void *memchr(const void *s, int c, size_t n) ;
库函数中对其的解释:The memchr() function scans the initial n bytes of the memory area pointed to by s for the first instance of c.
Both c and the bytes of the memory area pointed to by s are interpreted as unsigned char.
(memchr()函数扫描s指向的内存区域的初始n个字节,以找到c的第一个实例。c和s所指向的内存区域的字节都被解释为unsigned char。)
memchr是对内存进行操作的,且在进行操作时,遇到’\0’不会结束(即在遇到’\0’时,此时还未扫描完n个字节,不会停止,会继续与下一个内容比较),结束条件是当在n 个字节内没找到c时,退出返回null,找到c,则返回一个指向该内存c的位置。
实现原理:
//在参数 buf 所指向的字符串的前 n 个字节中搜索第一次出现字符 c(一个无符号字符)的位置。检测的是一段内存
//遇到\0不会终止
void *my_memchr(