函数 —— memset() 将存放字符串的数组清空后再赋予该数组新的字符串

void *memset(void *s, int ch, size_t n);

函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。

memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体数组进行清零操作的一种最快方法

#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! 

猜你喜欢

转载自blog.csdn.net/weixin_42167759/article/details/81123870