C Standard Library

C <wchar.h> - wmemchr() Function



The C <wchar.h> wmemchr() function is used to find the first occurrence of wc in first num wide characters of the wide character array pointed to by ptr.

The function returns pointer to the first occurrence of wc in ptr, or null wide pointer if wc is not found in ptr.

Syntax

const wchar_t* wmemchr (const wchar_t* ptr, wchar_t wc, size_t num);
wchar_t* wmemchr ( wchar_t* ptr, wchar_t wc, size_t num);

Parameters

ptr Specify pointer to the wide character array where the search is performed.
wc Specify the wide character to search for.
num Specify the maximum number of wide characters to be examined.
size_t is an unsigned integer type.

Return Value

Returns pointer to the first occurrence of ch in ptr, or null wide pointer if ch is not found in ptr.

Example:

The example below shows the usage of wmemchr() function.

#include <stdio.h>
#include <wchar.h>
 
int main (){
  wchar_t MyStr[100] = L"To be, or not to be, that is the question.";
  wchar_t * search;
  
  //searching for first occurrence of 'e' 
  //in first 25 wide characters of MyStr
  search = (wchar_t*) wmemchr(MyStr, L'e', 25);

  //displaying the result
  if(search != NULL)
    printf("Found at: %ld\n", (search-MyStr+1));
  else
    printf("Not Found.\n");
 
  return 0;
}

The output of the above code will be:

Found at: 5

❮ C <wchar.h> Library