问题描述
在标准C库的大多数实现中,
在先前分配的缓冲区中复制字符的函数,如strcpy或strcat,
返回指向该值的指针在我的NetBSD系统上,man strcpy给出了以下原型:
char * strcpy(char * restrict dst,const char * restrict src);
在电话会议之前返回我们已经知道的指针有什么意义?
提前感谢您的解释。
On most implementations of the standard C library, the functions that
copy characters in a previously allocated buffer, like strcpy or strcat,
are returning the pointer to that buffer.
On my NetBSD system, man strcpy gives the following prototype :
char *strcpy(char * restrict dst, const char * restrict src);
What''s the point in returning a pointer we already know before the call?
Thank you in advance for an explanation.
推荐答案
这样你就可以写(不必要的缩写) )这样的代码:
strcat(path,strcpy(file," fred.txt"));
So that you can write (unnecessarily abbreviated) code like this:
strcat(path, strcpy(file, "fred.txt"));
方便。其中一个函数调用的结果可能会被用作更大表达式的一部分,例如:
printf("%s \ n" ,strcat(s1,s2));
如果您不需要返回值,请忽略它。
strcat( s1,s2);
-Mike
Convenience. The result of one of these function calls might
be used as part of a larger expression, e.g.:
printf("%s\n", strcat(s1, s2));
If you don''t need the return value, just ignore it.
strcat(s1, s2);
-Mike
它让您对以下特有的运行时动作感到困惑:
char p [100] =" Fred";
....
printf("%s或%s \ n",p,strcat(p,"和George#));
对于改进的语义,查找(非标准)strlcat和
strlcpy函数。
-
Chuck F(cb********@yahoo.com)(cb ******** @ worldnet.att.net)
可用于咨询/临时嵌入式和系统。
< http://cbfalconer.home.att.net>使用worldnet地址!
It allows you to be baffled by the peculiar run-time actions of:
char p[100] = "Fred";
....
printf("%s or %s\n", p, strcat(p, " and George"));
For improved semantics look up the (non-standard) strlcat and
strlcpy functions.
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
这篇关于为什么strcpy,strcat,& co返回一个指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!