memset 还可以这样用

我们经常将memset用在初始化中,其实还可以这样方便的使用它;

给数组中的一部分初始化;

看例子:

#include<iostream>
#include<string.h>
using namespace std ;
int main()	{
	int a[21] = {0} ;
	memset(a+4,-1,5*sizeof(a[0])) ;
	for(int i = 0 ; i < 20 ; i++)
		cout << a[i] << ' ' ;
	cout << endl ;
	return 0 ;
}

  输出结果为;

0 0 0 0 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0

原文地址:https://www.cnblogs.com/scottding/p/3729471.html