问题描述
很多标准C库)的功能,特别是那些字符串处理,而最显着的strcpy(,分享的原型如下:
A lot of the functions from the standard C library, especially the ones for string manipulation, and most notably strcpy(), share the following prototype:
char *the_function (char *destination, ...)
这些函数的返回值实际上是一样的提供目标
。为什么你会浪费的东西冗余的返回值?它更有意义的这种功能为无效或返回有用的东西。
The return value of these functions is in fact the same as the provided destination
. Why would you waste the return value for something redundant? It makes more sense for such a function to be void or return something useful.
我唯一的猜测,为什么这是是,它更容易,更方便地窝函数调用的另一位前pression,例如:
My only guess as to why this is is that it's easier and more convenient to nest the function call in another expression, for example:
printf("%s\n", strcpy(dst, src));
还有没有其他合理的理由来证明这个成语?
Are there any other sensible reasons to justify this idiom?
推荐答案
作为埃文指出,这是可以做到像
as Evan pointed out, it is possible to do something like
char* s = strcpy(malloc(10), "test");
例如。分配的malloc()的
内存的值,而无需使用辅助变数。
e.g. assign malloc()ed
memory a value, without using helper variable.
(这个例子是不是最好的之一,它会崩溃在内存不足的情况,但这个想法是显而易见的)
(this example isn't the best one, it will crash on out of memory conditions, but the idea is obvious)
这篇关于的strcpy()的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!