_strnset
, _strnset_l
, _wcsnset
, _wcsnset_l
, _mbsnset
, _mbsnset_l
將字串字元初始化為指定的字元。 這些函式的更安全版本存在;請參閱 、、 _strnset_s_l
、 _wcsnset_s_l
_wcsnset_s
、 _mbsnset_s
、 _mbsnset_s_l
。 _strnset_s
重要
在 Windows 執行階段中執行的應用程式中無法使用 _mbsnset
和 _mbsnset_l
。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 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
是 Null 指標,則會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行,_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
另請參閱
字串操作
地區設定
多位元組字元序列的解譯
strcat
, wcscat
, _mbscat
strcmp
, wcscmp
, _mbscmp
strcpy
, wcscpy
, _mbscpy
_strset
, _strset_l
, _wcsset
, _wcsset_l
, _mbsset
, _mbsset_l
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應