函数 —— memset(给数组重新赋值,用指定字符替换数组中的部分值)
生活随笔
收集整理的這篇文章主要介紹了
函数 —— memset(给数组重新赋值,用指定字符替换数组中的部分值)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
描述
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:=Helloworle after:= 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總結
以上是生活随笔為你收集整理的函数 —— memset(给数组重新赋值,用指定字符替换数组中的部分值)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C 预处理器 —— __DATE__ #
- 下一篇: 计算机原理与基础 —— (皇帝身边的小太