曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4137|回复: 0
打印 上一主题 下一主题

[字符串] memchr() -- 在缓冲区里查找字符

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-9-5 09:18:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
函数原型如下:
[Plain Text] 纯文本查看 复制代码
void *memchr(const void *s, int c, size_t n);



第 1 个参数指向要字符串所在的缓冲区,第 2 个参数是要查找的字符串,第 3 个参数说明要在前 n 个字符串里查找。函数执行成功返回查找大的第 1 个匹配字符的位置,否则返回 NULL 。


示例:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>




int ch = 'w';
char string[] = "hello world";


int main()
{
    char *pdest;
    int result;
    printf ("string to be searched:\n\t\t%s\n", string);


    pdest = memchr (string, ch, sizeof(string));
    if (pdest != NULL) {
        result = pdest - string + 1;
        printf ("Result:\t\t%c found at position %d\n", ch, result);
    }else
        printf ("Result\t\t%c not found\n");


    return 0;
}


运行输出:
$ ./memchar
string to be searched:
        hello world
Result:        w found at position 7
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 23:19 , Processed in 0.081742 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表