memset, wmemset

將緩衝區設定為指定的字元。

語法

void *memset(
   void *dest,
   int c,
   size_t count
);
wchar_t *wmemset(
   wchar_t *dest,
   wchar_t c,
   size_t count
);

參數

dest
目的地的指標。

c
要設定的字元。

count
字元數。

傳回值

dest 的值。

備註

dest 的前 count 個字元設定為字元 c

安全性提示:確定目的緩衝區的空間足以容納至少 count 個字元。 如需詳細資訊,請參閱 避免緩衝區滿溢

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

需求

常式 必要的標頭
memset <memory.h><string.h>
wmemset <wchar.h>

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

程式庫

所有版本的 C 執行階段程式庫

範例

// crt_memset.c
/* This program uses memset to
* set the first four chars of buffer to "*".
*/

#include <memory.h>
#include <stdio.h>

int main( void )
{
   char buffer[] = "This is a test of the memset function";

   printf( "Before: %s\n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s\n", buffer );
}

該範例會產生以下輸出:

Before: This is a test of the memset function
After:  **** is a test of the memset function

以下是使用 的 wmemset 範例:

// crt_wmemset.c
/* This program uses memset to
* set the first four chars of buffer to "*".
*/

#include <wchar.h>
#include <stdio.h>

int main( void )
{
   wchar_t buffer[] = L"This is a test of the wmemset function";

   wprintf( L"Before: %s\n", buffer );
   wmemset( buffer, L'*', 4 );
   wprintf( L"After:  %s\n", buffer );
}

該範例會產生以下輸出:

Before: This is a test of the wmemset function
After:  **** is a test of the wmemset function

另請參閱

緩衝區操作
_memccpy
memchr, wmemchr
memcmp, wmemcmp
memcpy, wmemcpy
_strnset, _strnset_l, _wcsnset, _wcsnset_l, _mbsnset, _mbsnset_l