strcpy() 返回值

编程入门 行业动态 更新时间:2024-10-27 19:23:00
本文介绍了strcpy() 返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

标准 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, ...)

这些函数的返回值实际上与提供的destination相同.为什么要为多余的东西浪费返回值?将这样的函数设为 void 或返回有用的东西更有意义.

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.

我唯一的猜测是,将函数调用嵌套在另一个表达式中更容易、更方便,例如:

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 ", strcpy(dst, src));

是否有任何其他合理的理由来证明这个习语是正确的?

Are there any other sensible reasons to justify this idiom?

推荐答案

正如 Evan 指出的那样,可以做类似的事情

as Evan pointed out, it is possible to do something like

char* s = strcpy(malloc(10), "test");

例如为 malloc()ed 内存分配一个值,不使用辅助变量.

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() 返回值

本文发布于:2023-10-26 14:41:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   strcpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!