What is memset in cpp or C? Where it is used in cpp programs1 min read
void* memset( void* str, int ch, size_t n);
Parameters
str[] :
Pointer to the object to copy the character.
ch :
The character to copy.
n :
Number of bytes to copy.
Return value :
The memset() function returns str, the pointer to the destination string.
memset
(ans, 0,
sizeof
ans);
Here it is basically filling up whole ans array with 0 value.
You can fill up any array wit 0 or -1 using this method, but it doesn’t work for other numeric value.
But it can copy any character whatever you want to copy.
#include <cstring> #include <iostream> using namespace std; int main() { char str[] = "freshlybuilt"; memset(str, 'a', sizeof(str)); cout << str; return 0; }
Output:
aaaaaaaaaaaa
Reference:
Vishal Sharma Answered question