C库函数-memset

2021-07-24, updated 2021-09-12

C 库函数 void *memset(void *str, int c, size_t n) 复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符。

声明

下面是 memset() 函数的声明。

1
void *memset(void *str, int c, size_t n)

参数

返回值

该值返回一个指向存储区 str 的指针。

实例

下面的实例演示了 memset() 函数的用法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#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);
}

让我们编译并运行上面的程序,这将产生以下结果:

1
2
This is string.h library function
$$$$$$$ string.h library function
words: 239 tags: C string