_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l

将字符串的字符设置为一个字符。 提供这些函数的更安全版本;请参阅 _strset_s_strset_s_l_wcsset_s_wcsset_s_l_mbsset_s_mbsset_s_l

重要

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

语法

char *_strset(
   char *str,
   int c
);
char *_strset_l(
   char *str,
   int c,
   _locale_t locale
);
wchar_t *_wcsset(
   wchar_t *str,
   wchar_t c
);
wchar_t *_wcsset_l(
   wchar_t *str,
   wchar_t c,
   _locale_t locale
);
unsigned char *_mbsset(
   unsigned char *str,
   unsigned int c
);
unsigned char *_mbsset_l(
   unsigned char *str,
   unsigned int c,
   _locale_t locale
);

参数

str
要设置的 null 终止字符串。

c
字符设置。

locale
要使用的区域设置。

返回值

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

注解

_strset 函数将 str 的所有字符(终止 null 字符除外)设置为 c(已转换为 char)。 _wcsset_mbsset_l 分别是 _strset 的宽字符及多字节字符版本,而且参数和返回值的数据类型会相应地变化。 否则这些函数具有相同行为。

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

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些函数的版本都相同,只是不带 _l 后缀的函数使用当前区域设置,而带有 _l 后缀的函数则使用传入的区域设置参数。 有关详细信息,请参阅 Locale

重要

这些函数可能容易受到的缓冲区溢出的威胁。 缓冲区溢出可以用于系统攻击,因为它们可能使权限的提升不能确保。 有关详细信息,请参阅避免缓冲区溢出

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

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsset _strset _mbsset _wcsset
_tcsset_l _strset_l _mbsset_l _wcsset_l

要求

例程 必需的标头
_strset <string.h>
_strset_l <tchar.h>
_wcsset <string.h> 或 <wchar.h>
_wcsset_l <tchar.h>
_mbsset_mbsset_l <mbstring.h>

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

示例

// crt_strset.c
// compile with: /W3

#include <string.h>
#include <stdio.h>

int main( void )
{
   char string[] = "Fill the string with something.";
   printf( "Before: %s\n", string );
   _strset( string, '*' ); // C4996
   // Note: _strset is deprecated; consider using _strset_s instead
   printf( "After:  %s\n", string );
}
Before: Fill the string with something.
After:  *******************************

另请参阅

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