Sets buffers to a specified character.
void *memset(
void* dest,
int c,
size_t count
);
Example
/* MEMSET.C: This program uses memset to
* set the first four bytes of buffer to "*".
*/
#include <memory.h>
#include <stdio.h>
void 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 );
}
Output
Before: This is a test of the memset function
After: **** is a test of the memset function
In common use:
1 initialization a memory
Example
char str[100];
memset(str,0,100);
2清空一个结构类型的变量或数组
struct sample_struct
{
char csName[16];
int iSeq;
int iType;
};
对于变量
struct sample_strcut stTest;
一般情况下,清空stTest的方法:
stTest.csName[0]='/0';
stTest.iSeq=0;
stTest.iType=0;
用memset就非常方便:
memset(&stTest,0,sizeof(struct sample_struct));