memset() - Buffer Manipulation Function

How to fill memory block in C++?

Explanation

memset() buffer manipulation function fills the first "num" bytes of memory block pointed by the pointer "ptr" to the specified "value". It returns the "ptr" value.

Syntax:


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

Example :



#include <stdio.h>
#include <string.h>
int main ()
{
char str1[] = "wikipedia.org is an encylopedia";
memset (str1,'*',4);
puts (str1);
return 0;
}

Result :

****pedia.org is an encylopedia

In the above example, "memset()" is used to fill the first "4" bytes of the memory block "str1" with "*". This buffer manipulation function replaces the 4 characters by the symbol.

C++ Tutorial


Ask Questions

Ask Question