gpt4 book ai didi

c - strncmp 在解析器函数中失败

转载 作者:行者123 更新时间:2023-12-04 10:54:37 24 4
gpt4 key购买 nike

我找不到我的 parse() 函数中的问题。简短介绍:我从我的终端程序发送“boot\r\n”,调用了 parse() 函数,strncmp(buffer, "boot",4) 永远不会进入 if 分支?目前我只见树木不见森林。然后将从 parse() 函数返回的字符串提供给 remove_cr_lf_chars() 函数。我需要删除这些字符才能将字符串与图像名称进行比较,图像名称当然没有任何 '\r' 或 '\n' 字符。

static char *parse(void)
{
static char buffer[100];
char *p = buffer;
char c;

do {
while (!(USART1->SR & USART_FLAG_RXNE));
*p++ = c = ((char)USART1->DR);
} while (c != '\r' && p-buffer < sizeof buffer-1);
*p = '\0';

if (strncmp(buffer, "boot", 4))
{
p = buffer+4;
/* skip to argument, p might point to '\0' if none */
while (*p != '\0' && (*p == ' ' || *p == '\r')) p++;
return p;
}
return NULL;
}

static void remove_cr_lf_chars(char *dst)
{
for (; *dst != '\0'; dst++)
if (*dst != '\r' && *dst != '\n') dst++;
*dst = '\0';
}

最佳答案

这是因为 strncmp 在匹配时返回 0。你需要反转你的条件:

if (!strncmp(buffer, "boot", 4)) ... // Means "if buffer is equal to "boot" "

Return value: Returns an integral value indicating the relationship between the strings: A zero value indicates that the characters compared in both strings form the same string. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

关于c - strncmp 在解析器函数中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477571/

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