Use of the C language memset () function


C library function void * memset (void * str, int c, size_t n) to copy characters C (a unsigned char)
n characters pointed to the parameter string str.

Disclaimer
The following is a () function declaration memset.

void * memset (void * str, int c, size_t n)
Parameters
str - points to the memory block to be filled.
c - the value to be set. This value is passed as an int, but the function is used when filling the memory blocks form the unsigned char value.
n - the number of bytes to be set to that value.
Example: char A [100]; Memset (A, '/ 0', the sizeof (A));
Memset variable or array can be easily cleared of a structure type.

 

struct sample_struct
{
char csName[16];
int iSeq ;
int iType ;
} ;
int main()
{
struct sample_struct stTest;
//一般的情况stTest方法:
/*
stTest.csName[0]='\0';
stTest.iSeq=0;
stTest.iType=0;*/
memset(&stTest,0,sizeof(stTest));
printf("%c%d%d",stTest.csName[0],stTest.iSeq,stTest.iType);

//如果是数组
/*
struct sample_struct TEST[10];
memset(TEST,0,sizeof(struct sample_struct)*10);*/


/*
char str[50];
strcpy(str,"This is String.h library function!");
puts(str);
memset(str,'#',sizeof(char)*7);
puts(str);*/
}

Guess you like

Origin www.cnblogs.com/cocobear9/p/12571797.html