gpt4 book ai didi

C:为什么 strcpy 返回它的参数?

转载 作者:太空狗 更新时间:2023-10-29 17:24:23 26 4
gpt4 key购买 nike

为什么 strcpy(3)(和 strncpy(3))返回它们的第一个参数?我看不出这如何增加任何值(value)。相反,我经常希望返回复制的字节数。

附录:当我还需要结果字符串的长度时我应该怎么做?我真的必须实现自己的版本吗?

最佳答案

由于历史原因。 strcpy 和 friend 们可以追溯到七十年代初,我猜返回值的预期用例是一种链接:

// copy src into buf1 and buf2 in a single expression
strcpy(buf1, strcpy(buf2, src));

或者

char *temp = xmalloc(strlen(const_str) + 1);
function_that_takes_mutable_str(strcpy(temp, const_str));

关于C:为什么 strcpy 返回它的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com