1. Introduction to memset()
1.1 Function prototype
void *memset(void *s, int ch, size_t n);
1.2 Parameters
- s -pointer to the memory block.
- ch -the value used for replacement.
- n -the number of bytes to be replaced.
1.3 Function
Replace the n bytes after the current position in s with ch and return s.
The function is to fill a given value in a memory block, which is often used for clearing operations of larger structures and arrays.
1.4 Header files
#include <string.h>
2. Memset() usage
The memset function is often used to clear larger structures and arrays.
The specific code is as follows:
#include <stdio.h>
#include <string.h>
int main(void)
{
char data[7]="memset";
printf("%s\n",data);
memset(data,1,6);//全部置换为整数
printf("%d %d %d %d %d %d\n",data[0],data[1],data[2],data[3],data[4],data[5]);
memset(data,'a',6);//全部置换为字符
printf("%s\n",data);
memset(&data[4],1,2);//局部置换
printf("%d %d %d %d %d %d\n",data[0],data[1],data[2],data[3],data[4],data[5]);
return 0;
}
The results are as follows:
memset
1 1 1 1 1 1
aaaaaa
97 97 97 97 1 1