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. python中用ElementTree.iterparse()读取xml文件中的多层节点

    我在使用Python解析比较大型的xml文件时,为了提高效率,决定使用iterparse()方法,但是发现根据网上的例子:每次if event == 'end':之后elem.clear()或者是每次 ...

  2. java 异步调用与多线程

    异步与多线程的区别 一.异步和多线程有什么区别?其实,异步是目的,而多 线程是实现这个目的的方法.异步是说,A发起一个操作后(一般都是比较耗时的操作,如果不耗时的操作 就没有必要异步了),可以继续自顾 ...

  3. WebApi与Mvc的区别

    ASP.NET Mvc是基于ASP.NET管道设计的框架,但是Mvc应用程序只能在iis中运行,而WebApi框架既可以寄宿在IIs上也可以在Self-Host上进行处理. Mvc和webapi都有相 ...

  4. MFC学习(三):项目学习

    1. 概述 MFC程序由CWinApp.MainFrm(含Menu,可用CSplitterWndEx分割).众多Dialog等组成. MFC既可以使用纯Dialog的形式,也可以使用Document+ ...

  5. 【QTP专题】04_对象及操作方法

    本节介绍知识点包括 1.QTP自动化的原理 2.两类对象:TO(测试对象).RO(运行对象) 3.操作方法:SetTOProperty,GetROProperty,GetTOProperty 1.QT ...

  6. 爬虫开发8.scrapy框架之持久化操作

    今日概要 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...

  7. php数据库编程---mysqli扩展库

    1,mysqli扩展库允许我们访问MySQL数据库,并对MySql数据库进行curd操作.Mysqli扩展库比mysql扩展库加强了. 2,mysqli扩展库和mysql扩展库的比较 (1) mysq ...

  8. Python常用功能函数

    Python常用功能函数汇总 1.按行写字符串到文件中 import sys, os, time, json def saveContext(filename,*name): format = '^' ...

  9. Flink学习笔记:Flink Runtime

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  10. 【BZOJ1055】[HAOI2008]玩具取名(区间DP)

    [HAOI2008]玩具取名 题目描述 某人有一套玩具,并想法给玩具命名.首先他选择\(WING\)四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用" ...