C 庫函數 - memset()

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

描述

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

聲明

下麵是 memset() 函數的聲明。

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

參數

  • str -- 指向要填充的記憶體塊。
  • c -- 要被設置的值。該值以 int 形式傳遞,但是函數在填充記憶體塊時是使用該值的無符號字元形式。
  • n -- 要被設置為該值的位元組數。

返回值

該值返回一個指向存儲區 str 的指針。

實例

下麵的實例演示了 memset() 函數的用法。

#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

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