C++ Standard Library C++ STL Library

C++ <cwchar> - mbsinit() Function



The C++ <cwchar> mbsinit() function checks whether mbs points to an mbstate_t object representing an initial conversion state.

Zero-initialized value of mbstate_t represents the initial conversion state, although other values of mbstate_t may exist that also represent the initial conversion state. This function returns a non-zero value if mbs points to a mbstate_t object representing an initial conversion state, or if mbs is a null pointer. Otherwise, it returns a zero value. Calling this function never changes the state identified by mbs.

The state pointed by an mbstate_t object mbs can be set to the initial state by calling memset() function as given below:

memset(&mbs, 0, sizeof(mbs));              

Syntax

int mbsinit (const mbstate_t* mbs);

Parameters

mbs Specify pointer to an mbstate_t object.

Return Value

Returns a non-zero value if mbs points to a mbstate_t object representing an initial conversion state, or if mbs is a null pointer. Otherwise, returns a zero value.

Example:

The example below shows the usage on mbsinit() function.

#include <cstdio>
#include <cstring>
#include <cwchar>
 
int main (){
  char buff[50];
  mbstate_t mbs;
  wchar_t wcs[] = L"mbsinit Example";
  const wchar_t* ptr;

  ptr = wcs;

  //setting to initial state
  if (!mbsinit(&mbs))
    memset (&mbs,0,sizeof(mbs));

  wcsrtombs(buff, &ptr, 50, &mbs);
  printf("%s", buff);
 
  return 0;
}

The output of the above code will be:

mbsinit Example

❮ C++ <cwchar> Library