为什么strcpy返回char*而不是char
很多字符串函数都返回指针,但将指针返回到目的地和返回目的地有什么好处为什么strcpy返回char*而不是char,c,C,很多字符串函数都返回指针,但将指针返回到目的地和返回目的地有什么好处 #include <stdio.h> #include <stdlib.h> #include <string.h> char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination while ((*destination++ = *source++));
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination
while ((*destination++ = *source++));
*destination='\0';
return destination;
}
char sstrcpy2 ( char *destination, const char *source ){ //return destination
while ((*destination++ = *source++));
*destination='\0';
return *destination;
}
int main(void){
char source[] = "Well done is better than well said";
char destination[40];
sstrcpy ( destination, source );
printf ( "%s\n", destination);
return 0;
}
#包括
#包括
#包括
char*sstrcpy(char*destination,const char*source){//返回指向目标的指针
而((*目的地+=*源++);
*目的地='\0';
返回目的地;
}
char sstrcpy2(char*目的地,const char*源){//返回目的地
而((*目的地+=*源++);
*目的地='\0';
返回*目的地;
}
内部主(空){
char source[]=“说得好不如做得好”;
目的地字符[40];
sstrcpy(目的地、来源);
printf(“%s\n”,目的地);
返回0;
}
这样做的目的是提供链接功能的可能性。即,将一个函数结果作为参数传递给另一个函数结果
sstrcpy ( destination2, sstrcpy ( destination1, source ));
至于建议的sstrcpy2
,它只返回复制字符串的最后一个字符,在您的实现中显然是\0
,这在大多数情况下是无用的
更新:请注意,实现
sstrcpy
本身是不正确的,它将返回已移动到字符串末尾的destination
的值,而不是指向字符串开头的指针。或者,我建议保存原始指针并增加其副本:
char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination
char *dst = destination;
while ((*dst++ = *source++));
*dst='\0';
return destination;
}
错误1:返回字符串的结尾,而不是开头。
错误2:在末尾添加2个空终止符,而不是1个 正确的实施应遵循以下原则:
char* sstrcpy (char*restrict dst, const char*restrict src)
{
char* original = dst;
for(*dst = *src; *src != '\0'; dst++, src++)
{
*dst = *src;
}
return original;
}
其中,restrict
是与调用者签订的合同,该合同与dst
和src
不重叠。请注意,这可能仍然是一种幼稚的实现——对于小型微控制器来说,这很好,但对于32位系统的strcpy库实现,它们将使用对齐的数据块
返回指向目的地和返回目的地的指针有什么好处
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *sstrcpy ( char *destination, const char *source ){ //return a pointer to destination
while ((*destination++ = *source++));
*destination='\0';
return destination;
}
char sstrcpy2 ( char *destination, const char *source ){ //return destination
while ((*destination++ = *source++));
*destination='\0';
return *destination;
}
int main(void){
char source[] = "Well done is better than well said";
char destination[40];
sstrcpy ( destination, source );
printf ( "%s\n", destination);
return 0;
}
没有任何好处,;标准的图书馆里满是稀奇古怪的东西。这允许两种无意义的混淆:
// Bad code, do not use!
/*1*/ str = strcpy(str, src); // pointless and potentially dangerous
/*2*/ strcpy(str2, strcpy(str1, src)); // pointless and potentially dangerous
这反过来又允许参数评估中的副作用导致错误,因此这是危险的。别写那样的废话。正确的版本是:
/*1*/ strcpy(str, src);
/*2*/ strcpy(str1, src);
strcpy(str2, str1);
后面的版本更安全,更容易阅读。生成的机器代码将是相同的。
char
只能表示一个符号关于语句返回指向目标的指针并返回目标
,返回char不会返回整个字符串。它只返回单个字符(实际上是一个数字),这是该单个字符的ASCII表示形式。如果不返回指针(或字符数组),您将无法访问该字符串的其余字符。@jweyrich char sstrcpy2是否不正确?@jhonnna绝对正确。返回char
不仅不会返回整个字符串,而且C也不提供可以直接返回整个字符串的机制,因为字符串是(内容)数组和数组既不能传递给函数,也不能从函数返回。相反,数组内容的指针会被传递和/或返回,这正是您看到的。请注意,OP的两个实现都不正确,在您的示例中使用的第一个实现会将destination2
设置为空字符串,这与预期相反@rici为什么?目的地
是递增的,但它是按值传递的。最后一个字符设置为\0
,而不是第一个字符。更新:哦,等等。我发现问题是返回的值是指向字符串结尾而不是开头的指针,因此第二个sstrcpy
将从'\0'开始复制代码>。我认为(ir)的基本原理是你应该能够编写类似于str=strcpy(str,“hello”)的废话“u/CODE”,这类似于在同一个表达式中链接多个StrucPy调用一样愚蠢。请不要这样做。@ Lundin链接多个<代码> STRCPY 与 A= B= C < /C> >。即使具体示例只是为了容纳OPS示例。这些不必是相同的函数,而是一些STRIN。g函数。例如strcat
。我知道您刚刚受到OP的影响,但是strcpy
的参数是strcpy(dst,src)
。使用移动界面不是那么容易,否则我会有。