C 标准库 - string.h之memchr使用
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;
}
文章参考
- http://www.cplusplus.com/reference/cstring/memchr/
- http://zh.cppreference.com/w/c/string/byte/memchr
- http://www.runoob.com/cprogramming/c-function-memchr.html
转载注明出处
C 标准库 - string.h之memchr使用的更多相关文章
- C 标准库 - string.h
C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...
- C标准库<string.h>实现
本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...
- C标准库string.h中几个常用函数的使用详解
strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...
- C 标准库 - string.h之memmove使用
memmove Move block of memory Copies the values of num bytes from the location pointed by source to t ...
- C 标准库 - string.h之memcpy使用
memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...
- C 标准库 - string.h之memcmp使用
memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...
- C 标准库 - string.h之strlen使用
strlen Returns the length of the C string str. The length of a C string is determined by the termina ...
- C 标准库 - string.h之strpbrk使用
strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...
- C 标准库 - string.h之strrchr使用
strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...
随机推荐
- Java文件路径
几大常用的方法 Class.getResource("") 返回的是当前Class这个类所在包开始的位置 getClassLoader().getResource(" ...
- loadrunner中对https证书的配置
1.准备好网站的证书,一般证书是cer格式: 2.因为loadrunner只支持pem格式的证书,所以要将证书转换格式,利用openssl工具:(或者直接让开发提供pem格式的证书) 3.得到pe ...
- [示例] 使用 TStopwatch 计时
使用 TStopwatch 计时 uses System.Diagnostics; var t1: TStopwatch; begin t1 := TStopwatch.StartNew; // do ...
- ArcGis Android 10.2.6更新文档翻译
ArcGis Android 10.2.6更新文档翻译 @[ArcGis Android|10.2.6|更新文档] 本文描述了ArcGIS Runtime SDK for Android 10.2.6 ...
- Centos7 因内存 可用大小不足,被killed的解决办法
Linux的内存分配采取的是一种更加积极的分配策略,它假设应用申请了内存空间后并不会立即去使用它,所以允许一定量的超售,当应用真的需要使用它的时候,操作系统可能已经通过回收了其他应用的内存空间而变得有 ...
- 求帮忙解决封装jquery图片滚动问题
今天用jquery封装了点击图片滚动,但是发现在屏幕自适应时,图片停在的位置会随着屏幕大小而错位(我引入了pocketgrid.css响应式文件,但没办法去那边修改onsize事件...),求大神.. ...
- 免费的API
https://www.jianshu.com/p/e6f072839282 目前接口列表: 新实时段子 https://api.apiopen.top/getJoke?page=1&coun ...
- xshell无法连接Ubuntu的解决办法
使用workstations14安装完Ubunu后,网络连接方式为NAT模式(N):用于共享主机的IP地址 此时想用xshell连接此虚拟机但是提示连接失败,但是宿主机和虚拟机互相都能ping通,且虚 ...
- Qt keyPressEvent keyReleaseEvent 分析
最近使用Qt时,在增加一个按下某键(M),临时显示图层,键(M)弹起时隐藏图层的功能时,碰到了一些问题: keyPressEvent 事件不响应 这个问题,网上搜到的结果是可能是控件没获取焦点,比如Q ...
- 如何自动在Eclipse里对指定类或接口生成要覆盖的方法?
我们经常遇到对对指定类或接口生成要覆盖的方法,但是我们不了解这些类和接口,完全手写不能保证完全正确,那么是否可以准确知道指定类或接口要被覆盖的方法,自动生成这些代码呢?是的,完全可以,按照下面步骤操作 ...