memchr() - バッファーの検索

標準

標準/拡張機能 C/C++ 依存項目

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3

両方  

形式

#include <string.h>

void *memchr(const void *buf, int c, size_t count);

機能説明

memchr() 組み込み関数は、buf が指す最初 の count バイトを検索して、符号なし文字に 変換される c の最初のオカレンスを調べます。検索は、この関数が c を検出するか、count バイトを検査するまで続きます。

戻り値

正常に実行された場合、memchr() は、bufc のロケーションへのポインターを戻します。

cbuf の先頭の count バイトの中にない場合、memchr() は NULL を戻します。

CELEBM11
⁄* CELEBM11                                      

   This example finds the first occurrence of "x" in                            
   the string that you provide.                                                 
   If it is found, the string that starts with that character is                
   printed.                                                                     
   If you compile this code as MYPROG, then it could be invoked                 
   like this, with exactly one parameter:                                       
        MYPROG skixing                                                          
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
#include <string.h>                                                             
                                                                                
int main(int argc, char ** argv)                                                
{                                                                               
  char * result;                                                                
                                                                                
  if ( argc != 2 )                                                              
    printf( "Usage: %s string¥n", argv[0] );
  else                                                                          
  {                                                                             
    if ((result = (char *)memchr( argv[1], 'x', strlen(argv[1])) ) != NULL)     
      printf( "The string starting with x is %s¥n", result );
    else                                                                        
      printf( "The letter x cannot be found in the string¥n" );
  }                                                                             
}                                                                               
出力:
The string starting with x is xing

関連情報