memset() - 値へのバッファーの設定

標準

標準/拡張機能 C/C++ 依存項目

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3

両方  

形式

#include <string.h>

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

機能説明

memset() 組み込み関数は、dest の 先頭 count バイトを、符号なし int に 変換される c の値に設定します。

戻り値

memset() は、dest の値を戻します。

CELEBM15
⁄* CELEBM15                                      

   This example sets 10 bytes of the buffer to "A" and                          
   the next 10 bytes to "B".                                                    
                                                                                
 *⁄                                                                             
#include <string.h>                                                             
#include <stdio.h>                                                              
#define  BUF_SIZE     20                                                        
#define  HALF_BUF_SIZE   BUF_SIZE⁄2                                             
                                                                                
int main(void)                                                                  
{                                                                               
   char buffer[BUF_SIZE + 1];                                                   
   char *string;                                                                
                                                                                
   memset(buffer, 0, sizeof(buffer));                                           
   string = (char *)memset(buffer,'A', HALF_BUF_SIZE);                          
   printf("¥nBuffer contents: %s¥n", string);
   memset(buffer+HALF_BUF_SIZE, 'B', HALF_BUF_SIZE);                            
   printf("¥nBuffer contents: %s¥n", buffer);
}                                                                               
出力:
Buffer contents: AAAAAAAAAA

Buffer contents: AAAAAAAAAABBBBBBBBBB

関連情報