memchr

  • Locate character in block of memory,Searches within the first num bytes of the block of memory pointed by ptr for the first occurrence of ch (interpreted as an unsigned char), and returns a pointer to it.
  • 在参数 ptr 所指向的字符串的前 count 个字节中搜索第一次出现字符 ch(一个无符号字符)的位置。
  • Both ch and each of the bytes checked on the the ptr array are interpreted as unsigned char for the comparison.
  • 在 ptr 所指向对象的首 count 个字符(每个都转译成 unsigned char )中寻找 ch (在如同以 (unsigned char)ch 转换到 unsigned char 后)的首次出现。
  • 若访问出现于被搜索的数组结尾后,则行为未定义。
  • 若 ptr 为空指针则行为未定义。
  • 此函数表现如同它按顺序读取字符,并立即于找到匹配的字符时停止:
    • 若 ptr 所指向的字符数组小于 count ,但在数组中找到匹配,则行为良好定义。
void* memchr( const void* ptr, int ch, size_t count );

Parameters

ptr

  • Pointer to the block of memory where the search is performed.
  • 指向要检验的对象的指针

ch

  • ch to be located. The ch is passed as an int, but the function performs a byte per byte search using the unsigned char conversion of this ch.
  • 要搜索的字符
  • 以 int 形式传递的值,但是函数在每次字节搜索时是使用该值的无符号字符形式。

count

  • Number of bytes to be analyzed,size_t is an unsigned integral type.
  • 要检验的最大字符数

Return Value

  • A pointer to the first occurrence of ch in the block of memory pointed by ptr.

    If the ch is not found, the function returns a null pointer.
  • 指向字符位置的指针,或若找不到该字符则为 NULL 。
  • 该函数返回一个指向匹配字节的指针,如果在给定的内存区域未出现字符,则返回 NULL。

Example

//
// Created by zhangrongxiang on 2018/2/8 14:10
// File memchr
// //C 库函数 void *memchr(const void *str, int c, size_t n)
// 在参数 str 所指向的字符串的前 n 个字节中搜索第一次出现字符 c(一个无符号字符)的位置。 #include <string.h>
#include <stdio.h> int main() {
//字符串
char str[] = "I am your God";
//数组
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
char *position = (char *) memchr(str, 'y', strlen(str));
if (position) {
printf("%s\n", position);//your God
printf("%d\n", (int) (position - str));//5
printf("%c\n", str[position - str]); //y
} else {
printf("null\n");
}
int *pInt = (int *) memchr(arr, 50, sizeof(arr));
printf("%d\n", *pInt);//50
printf("%c\n", *pInt);//2
printf("%c\n", (unsigned char) 50); //2
printf("%c\n", (char) 50); //2
printf("%c\n", 50);//2
printf("%c\n", (unsigned char) 51); // 3 return 0;
}

文章参考

转载注明出处

C 标准库 - string.h之memchr使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  4. C 标准库 - string.h之memmove使用

    memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...

  5. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  6. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  7. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  8. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  9. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

随机推荐

  1. 12、Semantic-UI之输入框

    12.1 基础输入框   在Semantic-UI中可以定义多个样式的输入框,可以将图片与输入框结合,输入提示信息文字,设置输入框的状态. 示例:定义基础输入框 用户名: <div class= ...

  2. Java50道经典习题-程序2 输出素数

    题目:判断101-200之间有多少个素数,并输出所有素数 分析:判断素数的方法:用一个数分别去除2到(这个数-1)的数,如果能被整除,则表明此数不是素数,反之是素数. public class Pro ...

  3. [Erlang00]:gen_server:reply/2

    --- gen_server:reply/2 reply(Client, Reply) –> Result      Types:     Client - see below     Repl ...

  4. jsonp的使用记录

    最近前端的同事说要写一个手机查看的html5页面,需要我提供数据. 这个很ok啊,立马写了个服务返回数据.但是对方调用不了,因为跨域了. 返回错误如下:  Failed to load xxxxxx: ...

  5. python-自定义异步非阻塞爬虫框架

    api import socket import select class MySock: def __init__(self, sock, data): self.sock = sock self. ...

  6. linux命令之文件系统管理命令(下)

    1.mount:挂载文件系统 该命令可以将指定的文件系统挂载到指定目录(挂载点),在linux中必须先挂载所有的设备,才能够访问,挂载的目录必须事先存在并且最好为空. 参数 说明 -t(常用) 指定挂 ...

  7. python之爬虫--番外篇(一)进程,线程的初步了解

    整理这番外篇的原因是希望能够让爬虫的朋友更加理解这块内容,因为爬虫爬取数据可能很简单,但是如何高效持久的爬,利用进程,线程,以及异步IO,其实很多人和我一样,故整理此系列番外篇 一.进程 程序并不能单 ...

  8. collections中的defaultdict

    用类型 用函数返回值 嵌套的dict from collections import defaultdict def tree(): return defaultdict(tree) c = defa ...

  9. gitlab迁移版本库(保留原版本库的所有内容)

    如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作. 1) 从原地址克隆一份裸版本库,比如原本托管于 GitHub git clone --ba ...

  10. dos 下脚本编写须知

    主题为:doc脚本运行结束后,不自动退出终端. 方法1假设你的bat名字叫rabbit.bat你可以新开一个bat,内容是start rabbit.bat然后这个新的bat是不会自动关闭的 注意,这个 ...