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. SharpMap开发教程——图层标注

    在GIS开发中,根据图层属性字段对要素进行标注(图层标注)是一项常规的.必备的功能.在基于SharpMap开发GIS应用时,也可以方便的实现该功能. 1.加载Shapefile图层数据 SharpMa ...

  2. django 快捷代码提示

    1.右键项目,Mark Directory As Source Root 2.settings配置 3.import包时可忽略app名了

  3. Windows上编译Boost

    Boost做得很好,有自己的build系统,可以几乎一键式编译,这才是尼玛世界一流质量的良心开源库啊. 将Boost 1.49.0解压到boost/boost_1_49_0里面,然后在boost目录底 ...

  4. 虚拟机ping 不通主机,主机可ping 虚拟机解决方法

    在VMware虚拟机里安装了CentOS的系统发现桥接模式Ping不通外网,Ping主机也ping 不通,但是主机可以ping 虚拟机. 百度了以下,原因是w10防火墙搞的鬼,解决办法有两种: 1.关 ...

  5. 「HNOI 2015」亚瑟王

    \(Description\) 有\(n\)张卡牌,每一张卡牌有\(p_i\)的概率发动,并造成\(d_i\)点伤害.一共有\(r\)轮,每一轮按照编号从小到大依次考虑,如果这张牌已经发动过则跳过该牌 ...

  6. “全栈2019”Java第二十五章:流程控制语句中循环语句while

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. Servlet实现禁用cookie重写URL获取session

    前言 一个女人让他的程序员丈夫去商店买东西:你去附近的商店买些鸡蛋,如果有香蕉的话,买8个回来,这个丈夫买了8个鸡蛋回来,他的妻子大吃一惊:你为什么买了8个鸡蛋?! 程序员丈夫回答:因为他们有香蕉. ...

  8. tcp/ip学习笔记(1)-基本概念

    为什么会有tcp/ip 在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别.就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样.计 ...

  9. Python3.4程序异常判断

    实例代码[更多实例,请访问:www.yeayee.com] 1 #idle中按F5可以运行代码 2 #引入外部模块 import xxx 3 #random模块,randint(开始数,结束数) 产生 ...

  10. 十,PHP下载文件

    1,文件类型 (1)文本文件,如xx.txt. (2)二进制文件,如图片.视频.音频. 2,文件下载流程如下图所示,首先浏览器向服务器发送下载请求,服务器将下载资源读入内存,再通过http将资源返回到 ...