memset 函式學習

2021-08-26 12:53:23 字數 1403 閱讀 9691

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

總的作用:將已開闢記憶體空間 s 的首 n 個位元組的值設為值 c。

這個函式通常用於清空所指長度的的記憶體。。。。

下面的例子是讓所指的記憶體長度置為你想要的值

memset() 函式常用於記憶體空間初始化。如:

char str[100];

memset(str,0,100);

memset()的深刻內涵:用來對一段記憶體空間全部設定為某個字元,一般用在對定義的字串進行初始化為『 』或『/0』;例:char a[100];memset(a, '/0', sizeof(a));

memcpy用來做記憶體拷貝,你可以拿它拷貝任何資料型別的物件,可以指定拷貝的資料長度;例:char a[100],b[50]; memcpy(b, a, sizeof(b));注意如用sizeof(a),會造成b的記憶體位址溢位。

strcpy就只能拷貝字串了,它遇到'/0'就結束拷貝;例:char a[100],b[50];strcpy(a,b);如用strcpy(b,a),要注意a中的字串長度(第乙個『/0』之前)是否超過50位,如超過,則會造成b的記憶體位址溢位。

.補充:一點心得

memset可以方便的清空乙個結構型別的變數或陣列。

英文解釋可能會更好

fill block of memory

sets the first

num bytes of the block of memory pointed by

ptr to the specified

value (interpreted as an

unsigned char).

ptr

pointer to the block of memory to fill.

value

value to be set. the value is passed as an

int, but the function fills the block of memory using the

unsigned char conversion of this

value.

num

number of bytes to be set to the value.

/* memset example */ #include #include int main ()

output:

------ every programmer should know memset!

memset函式學習20190721

一 memset是計算機中c c 語言初始化函式。作用是將某一塊記憶體中的內容全部設定為指定的值,這個函式通常為新申請的記憶體做初始化工作。void memset void s,int ch,size t n 函式解釋 將s中當前位置後面的n個位元組 typedef unsigned int siz...

關於菜鳥學習memset 函式

畢業設計要用到乙個基本的單鏈表,今天寫了乙個單鏈表的函式,以前也是寫過很多遍了,憑著感覺沒怎麼想,就寫了點,可是蛋疼的錯誤要我想了好久啊 部分 如下 include link.h link cre link link insert link link head,link new node while...

memset 函式用法

memset 原型 extern void memset void buffer,int c,int count 用法 include 功能 把buffer所指記憶體區域的前count個位元組設定成字元c。說明 返回指向buffer的指標。用來對一段記憶體空間全部設定為某個字元。舉例 char a ...