关键词搜索

源码搜索 ×
×

memchr函数与strchr函数的区别

发布2014-03-13浏览9683次

详情内容

     先看看原型吧:

原型:extern void *memchr(const void *buf, int ch, size_t count);
用法:#include <string.h>
功能:从buf所指内存区域的前count个 字节查找 字符ch。
说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的 指针;否则返回NULL。

 

     看个程序吧:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char str[100] = "aaacbcxy";
  6. char *p = (char *)memchr(str, 'c', 10);
  7. if(NULL != p)
  8. {
  9. cout << *(p + 3) << endl;
  10. }
  11. else
  12. {
  13. cout << "no" << endl;
  14. }
  15. return 0;
  16. }

      结果为x


      再看下面的程序:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char str[100] = "aaacbcxy";
  6. char *p = strchr(str, 'c');
  7. if(NULL != p)
  8. {
  9. cout << *(p + 3) << endl;
  10. }
  11. else
  12. {
  13. cout << "no" << endl;
  14. }
  15. return 0;
  16. }
      结果一样。但是请不要忽视两个函数的差别。多的我就不说了,我只说有差别,提醒大家即可。至于具体有什么差别,你自己查阅一次,比较好。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载