C 庫函數(shù) - memset()

C 標準庫 - <string.h> C 標準庫 - <string.h>

描述

C 庫函數(shù) void *memset(void *str, int c, size_t n) 復制字符 c(一個無符號字符)到參數(shù) str 所指向的字符串的前 n 個字符。

聲明

下面是 memset() 函數(shù)的聲明。

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

參數(shù)

  • str -- 指向要填充的內(nèi)存塊。
  • c -- 要被設(shè)置的值。該值以 int 形式傳遞,但是函數(shù)在填充內(nèi)存塊時是使用該值的無符號字符形式。
  • n -- 要被設(shè)置為該值的字節(jié)數(shù)。

返回值

該值返回一個指向存儲區(qū) str 的指針。

實例

下面的實例演示了 memset() 函數(shù)的用法。

#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);
}

讓我們編譯并運行上面的程序,這將產(chǎn)生以下結(jié)果:

This is string.h library function
$$$$$$$ string.h library function

C 標準庫 - <string.h> C 標準庫 - <string.h>