MEMCMPC实现-此实现有任何逻辑错误吗
MEMCMPC实现-这个有逻辑错误吗 我正在寻找memcmp()的一个实现,我找到了这个代码段,但它清楚地标记了代码段中有1个逻辑错误。你能帮我找到逻辑错误吗 基本上,我使用不同的输入对memcmp()的string.h library实现测试了这段代码,但预期的输出始终与函数的库版本相同 以下是代码片段:MEMCMPC实现-此实现有任何逻辑错误吗,c,C,MEMCMPC实现-这个有逻辑错误吗 我正在寻找memcmp()的一个实现,我找到了这个代码段,但它清楚地标记了代码段中有1个逻辑错误。你能帮我找到逻辑错误吗 基本上,我使用不同的输入对memcmp()的string.h library实现测试了这段代码,但预期的输出始终与函数的库版本相同 以下是代码片段: #include <stdio.h> #include <string.h> int memcmp_test(const char *cs, const char
#include <stdio.h>
#include <string.h>
int memcmp_test(const char *cs, const char *ct, size_t n)
{
size_t i;
for (i = 0; i < n; i++, cs++, ct++)
{
if (*cs < *ct)
{
return -1;
}
else if (*cs > *ct)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
int ret_val = 20; //initialize with non-zero value
char *string1 = "china";
char *string2 = "korea";
ret_val = memcmp_test(string1,string2,5);
printf ("ret_val is = %d",ret_val);
getchar();
return 0;
}
#包括
#包括
int memcmp_测试(常量字符*cs,常量字符*ct,大小)
{
尺寸i;
对于(i=0;i*ct)
{
返回1;
}
其他的
{
返回0;
}
}
}
int main()
{
int ret_val=20;//使用非零值初始化
char*string1=“中国”;
char*string2=“韩国”;
ret_val=memcmp_试验(第1条、第2条、第5条);
printf(“返回值为=%d”,返回值);
getchar();
返回0;
}
我使用两个示例字符串运行程序,程序将在比较两个字符串的第一个字符后返回。在上述情况下,ret_val为-1
上述代码段应符合的memcmp()定义为:
“C”库函数memcmp的定义为
int memcmp(常量字符*cs,常量字符*ct,大小n)
比较cs的前n个字符与ct的前n个字符。
如果cs肯定有逻辑错误,你能帮我找到它吗。看看你的for循环。它只检查一个字符。严格来说,签名是错误的。一个是: 您的代码将
c
和k
进行比较,发现c
小于k
时,尽职地返回-1。然而,如果这两者相等,你会得到一个不正确的结果,因为你很早就回来了
如果您阅读文档,您会发现:
非零返回值的符号应由第一对字节(均解释为类型unsigned char)值之间的差值符号确定,这两个字节值在被比较的对象中不同
这基本上意味着您通过返回保留('c'-'k')
符号的内容来做正确的事情
可以找到一个更简单的实现。我想既然
char
signedness是实现定义的,您可以进行比较unsigned
:
int memcmp_test(const char *cs_in, const char *ct_in, size_t n)
{
size_t i;
const unsigned char * cs = (const unsigned char*) cs_in;
const unsigned char * ct = (const unsigned char*) ct_in;
for (i = 0; i < n; i++, cs++, ct++)
{
if (*cs < *ct)
{
return -1;
}
else if (*cs > *ct)
{
return 1;
}
}
return 0;
}
int memcmp_测试(常量字符*cs_英寸,常量字符*ct_英寸,大小)
{
尺寸i;
常量无符号字符*cs=(常量无符号字符*)cs_in;
常量无符号字符*ct=(常量无符号字符*)ct_in;
对于(i=0;i*ct)
{
返回1;
}
}
返回0;
}
正如现在编写的,此代码将只测试输入的第一个字节。需要将else返回0
移出循环,将return 0
保留在末尾:
for (i = 0; i < n; i++, cs++, ct++)
{
if (*cs < *ct)
{
return -1;
}
else if (*cs > *ct)
{
return 1;
}
}
return 0;
}
(i=0;i
{
如果(*cs<*ct)
{
返回-1;
}
否则,如果(*cs>*ct)
{
返回1;
}
}
返回0;
}
返回0;仅在比较第一个字符后发生。它应该放在循环之外。这段代码非常适合我
#include <stdio.h>
#include <string.h>
int memcmp_test(const char *cs, const char *ct, size_t n)
{
size_t i;
for (i = 0; i < n; i++, cs++, ct++)
{
if (*cs < *ct)
{
return -1;
}
else if (*cs > *ct)
{
return 1;
}
}
return 0;
}
int main()
{
int ret_val = 20; //initialize with non-zero value
const char *string1 = "DWgaOtP12df0";
const char *string2 = "DWGAOTP12DF0";
ret_val = memcmp_test(string1,string2,5);
printf ("ret_val is = %d",ret_val);
getchar();
return 0;
}
#包括
#包括
int memcmp_测试(常量字符*cs,常量字符*ct,大小)
{
尺寸i;
对于(i=0;i*ct)
{
返回1;
}
}
返回0;
}
int main()
{
int ret_val=20;//使用非零值初始化
const char*string1=“DWgaOtP12df0”;
const char*string2=“DWGAOTP12DF0”;
ret_val=memcmp_试验(第1条、第2条、第5条);
printf(“返回值为=%d”,返回值);
getchar();
返回0;
}
这是家庭作业吗?我只想说看看“return 0”案例,想想它的作用和时间。还要注意mehrdad的答案。或者执行(unsigned char)*cs<(unsigned char)*ct样式的比较nshmm,尽管我认为即使是(unsigned char)*cs<(unsigned char)*ct也可能无法处理符号大小的有符号char实现。使用该无符号字符比较,0x81(=>-1)将高于0x82(=>-2)。但事实上,当解释为无符号字符时,0x81当然小于0x82。因此,我想正确/可移植的唯一方法可能是按照mehrdad所示或执行(无符号字符)cs<(无符号字符)ct。那是一只狡猾的野兽@Johanneschaub litb:或者更明显地,比较+0和-0是否相等:)@litb:谢谢,我认为它也会在没有强制转换的情况下编译(可能是警告)。你是这里的C/C++权威。不用客气;)+1注意这个微妙的问题:memcmp()
应该进行无符号比较!另外,我认为if(*cs-*ct){return*cs-*ct;}
由于提升到int是安全的,假设int
保证至少有一个额外的位。
#include <stdio.h>
#include <string.h>
int memcmp_test(const char *cs, const char *ct, size_t n)
{
size_t i;
for (i = 0; i < n; i++, cs++, ct++)
{
if (*cs < *ct)
{
return -1;
}
else if (*cs > *ct)
{
return 1;
}
}
return 0;
}
int main()
{
int ret_val = 20; //initialize with non-zero value
const char *string1 = "DWgaOtP12df0";
const char *string2 = "DWGAOTP12DF0";
ret_val = memcmp_test(string1,string2,5);
printf ("ret_val is = %d",ret_val);
getchar();
return 0;
}