memset 用法

来源:互联网 发布:兰州seo 编辑:程序博客网 时间:2024/06/10 03:51

在你申请了一块内存之后,
比如
int *p=NULL;
p=malloc(10*sizeof(int));//申请了10个int型内存
memset(p,0,10*sizeof(int));//全部初始化为0

memset的作用就是把你快连续的内存初始化为你给的值。
Example
/* MEMSET.C: This program uses memset to
* set the first four bytes of buffer to "*".
*/

#include <memory.h>
#include <stdio.h>

void main( void )
{
   char buffer[] = "This is a test of the memset function";

   printf( "Before: %s/n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s/n", buffer );
}

Output
Before: This is a test of the memset function
After:  **** is a test of the memset function