What's the use of memset() return value?

岁酱吖の 提交于 2019-12-03 10:21:03

问题


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?


回答1:


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.




回答2:


It may be used for call chaining like:

char a[200];
strcpy(memset(a, 0, 200), "bla");



回答3:


In order to use the function as an argument for another function such as sprintf




回答4:


I came across this question when Googling to see what memset returned.

I have some code where I test for one value, then if that is true test to see if a value is zeros.

Because there is no completely portable way in C to test for zeros I have 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 to the chaining purpose listed in the previous questions, but it is an example of a use for this technique.

I'll leave it to others to judge if this is good coding or not.



来源:https://stackoverflow.com/questions/13720428/whats-the-use-of-memset-return-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!