_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l

将字符串的字符初始化为给定字符。 这些函数存在更安全版本;请参阅 _strnset_s_strnset_s_l_wcsnset_s_wcsnset_s_l_mbsnset_s_mbsnset_s_l

重要

_mbsnset_mbsnset_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

char *_strnset(
   char *str,
   int c,
   size_t count
);
char *_strnset_l(
   char *str,
   int c,
   size_t count,
   _locale_t locale
);
wchar_t *_wcsnset(
   wchar_t *str,
   wchar_t c,
   size_t count
);
wchar_t *_wcsnset_l(
   wchar_t *str,
   wchar_t c,
   size_t count,
   _locale_t locale
);
unsigned char *_mbsnset(
   unsigned char *str,
   unsigned int c,
   size_t count
);
unsigned char *_mbsnset_l(
   unsigned char *str,
   unsigned int c,
   size_t count,
   _locale_t locale
);

参数

str
要修改的字符串。

c
字符设置。

count
要设置的字符数。

locale
要使用的区域设置。

返回值

返回指向修改后的字符串的指针。

备注

_strnset 函数最多可以将 str 的前 count 个字符设置为 c(已转换为 char)。 如果 count 大于 str 的长度,则会使用 str 的长度而使用 count

_wcsnset_mbsnset 分别是 _strnset 的宽字符及多字节字符版本。 _wcsnset 的字符串自变量和返回值为宽字符字符串。 _mbsnset 的字符串自变量和返回值为多字节字符字符串。 否则这三个函数否则具有相同行为。

_mbsnset 验证其参数;如果 str 为空指针,则调用无效的参数处理程序,如参数验证中所述。 如果允许继续执行,则 _mbsnset 返回 NULL,并将 errno 设置为 EINVAL_strnset_wcsnset 不会验证其参数。

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些不带 _l 后缀的函数的版本使用为该区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。 有关详细信息,请参阅 Locale

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsnset _strnset _mbsnbset _wcsnset
_tcsnset_l _strnset_l _mbsnbset_l _wcsnset_l

要求

例程 必需的标头
_strnset <string.h>
_strnset_l <tchar.h>
_wcsnset <string.h> 或 <wchar.h>
_wcsnset_l <tchar.h>
_mbsnset_mbsnset_l <mbstring.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strnset.c
// compile with: /W3
#include <string.h>
#include <stdio.h>

int main( void )
{
   char string[15] = "This is a test";
   /* Set not more than 4 characters of string to be *'s */
   printf( "Before: %s\n", string );
   _strnset( string, '*', 4 ); // C4996
   // Note: _strnset is deprecated; consider using _strnset_s
   printf( "After:  %s\n", string );
}
Before: This is a test
After:  **** is a test

另请参阅

字符串操作
区域设置
多字节字符序列的解释
strcatwcscat_mbscat
strcmpwcscmp_mbscmp
strcpywcscpy_mbscpy
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l