ds第一次作业

memset函数

(一)函数定义原型

void * memset ( void * ptr, int value, size_t num )

  

头文件:string.h或cstring
格式:memset(指向任意类型的指针,所要赋的值,sizeof(指针))
功能:初始化或者清空

(二)使用样例

(1)初始化

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include<set>
#include<cstring>
using namespace std;
#define maxn 1005


int main()
{
char buffer[] = "this is a string!";

memset(buffer,'0',4);

printf("%s",buffer);

return 0;
}

 

结果为0000 is a string

(2)清空

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include<set>
#include<cstring>
using namespace std;
#define maxn 1005


int main()
{
char buffer[] = "this is a string!";

memset(buffer,0,sizeof(buffer));

printf("%s",buffer);
return 0;
}

 

结果为空

(三)经验总结

memset一般多用于初始化,特别是在新建一维数组二维数组的时候为防止数据乱码而初始化0或inf=0x3f3f3f,特别有用。

posted @ 2019-04-19 20:26  天生要强注定要凉  阅读(121)  评论(0)    收藏  举报