memset()
is declared to return void*
that is always the same value as the address passed into the function.
What's the use of the return value? Why does it not return void
?
memset() is used to fill a block of memory with a particular value. The syntax of memset() function is as follows : // ptr ==> Starting address of memory to be filled // x ==> Value to be filled // n ==> Number of bytes to be filled starting // from ptr to be filled void *memset(void *ptr, int x, size_t n);
memset() is built in standard string function that is defined in string header library string.
memset() will blindly write to the specified address for the number of specified bytes, regardless of what it might be overwriting. It is up to the programmer to ensure that only valid memory is written to.
Memset() is a C++ function. It copies a single character for a specified number of times to an object. It is defined in <cstring> header file.
It may be used for call chaining like:
char a[200]; strcpy(memset(a, 0, 200), "bla");
The signature is in line with all the other similar functions: memcpy()
, strcpy()
etc. I always thought this was done to enable one to chain calls to such functions, and to otherwise use such calls in expressions.
That said, I've never come across a real-world situation where I would feel compelled to use the return value in such a manner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With