What use of memset () returns a value?

memset() declared as a return void* , which always matches the address passed to the function.

What is the use of return value? Why doesn't it return void ?

+85
c ++ c memset
Dec 05
source share
4 answers

The signature matches all other similar functions: memcpy() , strcpy() , etc. I always thought that this was done so that you could associate calls with such functions and otherwise use such calls in expressions.

However, I have never encountered a situation in the real world where I would be forced to use the return value in this way.

+33
Dec 05
source share

It can be used to chain calls, for example:

 char a[200]; strcpy(memset(a, 0, 200), "bla"); 
+115
Dec 05
source share

To use a function as an argument to another function such as sprintf

+12
Dec 05
source share

I came across this question when Googling looked that the memset was back.

I have a code where I test a single value, then if it is a true test to see if the value is zeros.

Since there is no fully portable way to check for zero in C, I need to run memset in the middle.

So my code is:

 if ( a==true && (memcmp(memset(zeros, 0, sizeof(zeros)), b, sizeof(zeros)) == 0) ) 

This speaks of the chain of goals listed in previous questions, but this is an example of use for this technique.

I will leave it to others to judge whether this encoding is good or not.

0
May 20 '13 at 3:29
source share



All Articles