描述
C 库函数void *memset(void *str, int c, size_t n)复制字符c(一个无符号字符)到参数str所指向的字符串的前n个字符。
参数
str-- 指向要填充的内存块。c-- 要被设置的值。该值以 int 形式传递,但是函数在填充内存块时是使用该值的无符号字符形式。n-- 要被设置为该值的字节数。实例1 —— 给数组重新赋值
#include <string.h>#include <stdio.h>#include <memory.h>int main(void){char buf[] = "Helloworle";char str[]="heihie!";printf("before:=%s\n",buf);;memset(buf,0,strlen(buf));printf("after:=%s\n",buf);strcat(buf,str);printf("after strcat:= %s\n",buf);return 0;}
让我们编译并运行上面的程序,这将产生以下结果:
before:=Helloworleafter:=after strcat:= heihie!
实例2 —— 替换数组中的部分值
#include <stdio.h>#include <string.h>int main (){char str[50];strcpy(str,"This is string.h library function");puts(str);memset(str,'$',7);puts(str);return(0);}
让我们编译并运行上面的程序,这将产生以下结果:
This is string.h library function$$$$$$$ string.h library function