2022年6月10日星期五

C memchr

void    *memchr(const void *destString, char c, size_t n);

在從destString開始搜尋n個位元組內尋找c第一次出現的位址並返回,若未找到則返回NULL

  1. /*This file use to record string.h library use */
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. char HelloBuf[6]="Hello\n";//show chars
  6.  
  7. int main()
  8. {
  9. char findChar = 'A';
  10. int AddrNum = memchr(HelloBuf,findChar,6);//取得第一個'l'的位址,memchr(儲存空間,指定字元,搜索長度)
  11. int postion = AddrNum-(int)(&HelloBuf);
  12.  
  13. printf("HelloBuf:%s\n",HelloBuf);
  14. printf("findChar:%c\n",findChar);
  15. printf("HelloBuf:%p\n",&HelloBuf);//印出HelloBuf的位址
  16. printf("AddrNum:%p\n",memchr(HelloBuf,findChar,6));//印出'l'在HelloBuf中的位址
  17. printf("postion:%d\n",postion);//印出'l'所在HelloBuf中第幾個
  18.  
  19. return 0;
  20. }
  21.  

有找到字元"l"的結果


將findChar改為’ A ‘進行測試的結果,可以發現AddrNum為0

沒有留言:

發佈留言

打賞按讚