以下内容源于网络资源的学习与整理,欢迎交流。
函数原型:void *memset(void *s, int c, size_t n);
函数作用:将指针s所指向的内存的前n个字节的内容设置为c。
补充说明:用于初始化新申请的内存,它是对较大结构体或数组清零的一种最快的方法。
代码举例:
#include <string.h>
#include <stdio.h>
#include <memory.h>
int main(void)
{char buffer[] = "Hello world!";printf("Buffer before memset:\n %s\n", buffer);memset(buffer, '*', strlen(buffer) );printf("Buffer after memset:\n %s\n", buffer);return 0;
}
输出为:
root@ubuntu:/home/xjh/iot/embedded_basic/rootfs/tmp# gcc test.c
root@ubuntu:/home/xjh/iot/embedded_basic/rootfs/tmp# ./a.out
Buffer before memset:Hello world!
Buffer after memset:************
root@ubuntu:/home/xjh/iot/embedded_basic/rootfs/tmp#