_strset_s, _strset_s_l, _wcsset_s, _wcsset_s_l, _mbsset_s, _mbsset_s_l

將字串字元設定為字元。 這些版本的 _strset_strset_l_wcsset_wcsset_l_mbsset 具有 _mbsset_l 安全性增強功能,如 CRT 的安全性功能中所述

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbsset_s_mbsset_s_l。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

errno_t _strset_s(
   char *str,
   size_t numberOfElements,
   int c
);
errno_t _strset_s_l(
   char *str,
   size_t numberOfElements,
   int c,
   _locale_t locale
);
errno_t _wcsset_s(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c
);
errno_t *_wcsset_s_l(
   wchar_t *str,
   size_t numberOfElements,
   wchar_t c,
   _locale_t locale
);
errno_t _mbsset_s(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c
);
errno_t _mbsset_s_l(
   unsigned char *str,
   size_t numberOfElements,
   unsigned int c,
   _locale_t locale
);

參數

str
以 Null 終止的待設定字串。

numberOfElements
str 緩衝區的大小。

c
字元設定。

locale
要使用的地區設定。

傳回值

如果成功則為零,否則為錯誤碼。

這些函式會驗證它們的引數。 如果 str 是 Null 指標,或 numberOfElements 引數小於或等於 0,或傳入的區塊不會以 Null 終止,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 EINVAL,並將 errno 設為 EINVAL

備註

_strset_s 函式會將所有的 str 字元設成 c (轉換成 char),終止的 Null 字元除外。 _wcsset_s_mbsset_s 分別是 _strset_s 的寬字元版本和多位元組字元版本。 引數和傳回值的資料類型會隨之改變。 除此之外,這些函式的行為相同。

輸出值會受到 LC_CTYPE 地區設定之類別設定的影響。 如需詳細資訊,請參閱setlocale。 這些沒有 _l 後置字元的函式版本,會針對此與地區設定相關的行為使用目前的地區設定;具有 _l 後置字元的版本也一樣,只不過它們會改用傳遞的地區設定參數。 如需詳細資訊,請參閱 Locale

這些函式的偵錯程式庫版本會先將緩衝區填入0xFE。 若要停用此行為,請使用 _CrtSetDebugFillThreshold

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcsset_s _strset_s _mbsset_s _wcsset_s
_tcsset_s_l _strset_s_l _mbsset_s_l _wcsset_s_l

需求

常式 必要的標頭
_strset_s <string.h>
_strset_s_l <tchar.h>
_wcsset_s <string.h > 或 < wchar.h>
_wcsset_s_l <tchar.h>
_mbsset_s, _mbsset_s_l <mbstring.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_strset_s.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char string[] = "Fill the string with something.";
   printf( "Before: %s\n", string );
   _strset_s( string, _countof(string), '*' );
   printf( "After:  %s\n", string );
}
Before: Fill the string with something.
After:  *******************************

另請參閱

字串操作
地區設定
多位元組字元序列的解譯
_mbsnbset, _mbsnbset_l
memset, wmemset
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l