gpt4 book ai didi

c - Strncmp - 错误的逻辑

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:25 26 4
gpt4 key购买 nike

在运行下面的代码时,我看到出现了 "x,y" 模式。此模式正在此处打印:printf("%s", start);。但是 if (strncmp(cp, VAL, strlen(VAL) == 0 )) 没有介入。我希望它应该介入。怎么了?

   me@host ~ $ ./test|grep x,y
<P k="x,y" v="160.59" z="100"/>

这是部分代码:

 #define VAL                       "\"x,y\""

void process(char * start, char *stop)
{
char * cp;
printf("process\n");
printf("%s", start);
for (cp = start; cp <= stop; cp++)
{
if (strncmp(cp, VAL, strlen(VAL) == 0 ))
{
printf ("F O U N D VAL\n");
}

最佳答案

也许你的意思是

if (strncmp(cp, VAL, strlen(VAL)) == 0)

相反

if (strncmp(cp, VAL, strlen(VAL) == 0 ))

基本上检查 0 个字符,返回 0,因此条件始终为 false

关于c - Strncmp - 错误的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20228528/

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