C 参考手册

定义于头文件 <wchar.h>
int mbsinit( const mbstate_t* ps);
(C95 起)

ps 不是空指针,则 mbsinit 函数确定其所指向的 mbstate_t 对象是否描述初始迁移状态。

注意

尽管零初始化的 mbstate_t 始终表示初始转换状态,亦可能有其他 mbstate_t 值表示初始转换状态。

参数

ps - 指向要检验的 mbstate_t 对象的指针

返回值

ps 不是空指针且不表示初始迁移状态则为 0 ,否则为非零。

示例

#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <wchar.h>
 
int main(void)
{
    // 允许mbrlen()作用于UTF-8多字节字符串
    setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    const char* str = u8"水"; // 或 u8"\u6c34" 或 "\xe6\xb0\xb4"
    static mbstate_t mb; // 零初始化
    (void)mbrlen(&str[0], 1, &mb);
    if (!mbsinit(&mb)) {
        printf("After processing the first 1 byte of %s,\n"
               "the conversion state is not initial\n\n", str);
    }
 
    (void)mbrlen(&str[1], strlen(str), &mb);
    if (mbsinit(&mb)) {
        printf("After processing the remaining 2 bytes of %s,\n"
               "the conversion state is initial conversion state\n", str);
    }
}

输出:

After processing the first 1 byte of 水,
the conversion state is not initial
 
After processing the remaining 2 bytes of 水,
the conversion state is initial conversion state

引用

  • C11 standard (ISO/IEC 9899:2011):
  • 7.29.6.2.1 The mbsinit function (p: 441-442)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.24.6.2.1 The mbsinit function (p: 387-388)

参阅

迭代多字节字符串所需的转换信息
(结构体)