gpt4 book ai didi

c - 为什么我需要这条线,据我所知它是多余的

转载 作者:行者123 更新时间:2023-11-30 17:58:51 25 4
gpt4 key购买 nike

下面是我用于解析标记的一段代码。

底部附近有一行用 >>>> 指示,不再需要,但如果我将其注释掉,cmd_parse_value_lookup() 函数将失败。如果我把它留在里面,代码就会正常运行。谁能告诉我原因,并解释发生了什么?

void cmd_parse(void)
{
cmd_parse_value=0;
int cmd_parse_counter = 1;
char *cmd_parse_pointer;
cmd_parse_pointer = strtok(cmd_buffer_in, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_parse_value_lookup(cmd_parse_pointer);
}
while (cmd_parse_pointer != NULL)
{
cmd_parse_counter++;
cmd_parse_pointer = strtok(NULL, " ");
if (cmd_parse_pointer!=NULL)
{
>>>>cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"%i: %s\r\n", cmd_parse_counter, cmd_parse_pointer); //WHY DO I NEED THIS LINE
cmd_parse_value_lookup(cmd_parse_pointer);
}
}
}

void cmd_parse_value_lookup(char *cmd_command)
{
if (strcmp(cmd_command,"show")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"get")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"set")==0)
{
cmd_parse_value |= 2;
}
else if (strcmp(cmd_command,"system")==0)
{
cmd_parse_value |= 4;
}
}

编辑:这是完整的代码:

/** C O M M A N D ************************************************************/

#include "generic.h"
#include <stdio.h>
#include <string.h>

void btm_out_character (char character);
void btm_out_string(char *string);
void cmd_parse(void);

char cmd_buffer_in[81]="\0 ";
int cmd_buffer_in_position=0;
unsigned long long cmd_parse_value=0;

char cmd_buffer_sprintf[81];
int cmd_buffer_sprintf_return;

void cmd_parse_value_lookup(char *cmd_command);
void cmd_buffer_in_add(int character);

void cmd_init(void)
{

}

void cmd_cls(void)
{
if (dbg_mode==1){btm_out_string("\033[2J\033[1;33;40m\033[H\r\n");}
if (dbg_mode==1){btm_out_string("================================================================================");}
if (dbg_mode==1){btm_out_string("\033[24;0H================================================================================");}
if (dbg_mode==1){btm_out_string("\033[1;36;40m\033[8;66H DEBUG MODE ");}
if (dbg_mode==1){btm_out_string("\033[24;7H ***** ******** ********** Engineering Limited, All Rights Reserved ");}
if (dbg_mode==1){btm_out_string("\033[10;0H");}
}

void cmd_prompt(void)
{
cmd_buffer_in_position = 0; // Clear buffer position
cmd_buffer_in[0]=0; // Clear buffer
if (dbg_mode==1){btm_out_string("\r\n\033[1;37;40m***:> ");}
}

void cmd_buffer_in_add(int character)
{
switch (character)
{
case 8:
if (cmd_buffer_in_position>0)
{
if (dbg_mode==1){btm_out_string("\b \b");}
cmd_buffer_in[(int)cmd_buffer_in_position-1] = (char)character;
cmd_buffer_in[(int)cmd_buffer_in_position] = 0;
cmd_buffer_in_position--;
}
else
{
if (dbg_mode==1){btm_out_character(7);} //BELL alert (too long a string)
cmd_buffer_in[0] = 0;
cmd_buffer_in_position = 0;
}
break;
case 13:
cmd_parse();
cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"\r\nparse value=%llu\r\n", cmd_parse_value);
if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}
cmd_prompt();
break;
default:
if (cmd_buffer_in_position<73)
{
if (dbg_mode==1){btm_out_character((char)character);} //Echo character to host
cmd_buffer_in[(int)cmd_buffer_in_position] = (char)character;
cmd_buffer_in[(int)cmd_buffer_in_position+1] = 0;
cmd_buffer_in_position++;
}
else
{
if (dbg_mode==1){btm_out_character(7);} //BELL alert (too long a string)
}
}//switch
}//cmd_buffer_in_add

void cmd_parse(void)
{
cmd_parse_value=0;

/* this was code for testing */
//cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"\r\nClone %s",cmd_buffer_in);
//cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"Returnval='%i''%i''%i'",cmd_buffer_in[(int)0],cmd_buffer_in[(int)1],cmd_buffer_in[(int)2]);
//if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}

int cmd_parse_counter = 1;
char *cmd_parse_pointer;

cmd_parse_pointer = strtok(cmd_buffer_in, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_parse_value_lookup(cmd_parse_pointer);
}
while (cmd_parse_pointer != NULL)
{
cmd_parse_counter++;
cmd_parse_pointer = strtok(NULL, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"%i: %s\r\n", cmd_parse_counter, cmd_parse_pointer); //WHY DO I NEED THIS LINE
//if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}
cmd_parse_value_lookup(cmd_parse_pointer);
}
}
}

void cmd_parse_value_lookup(char *cmd_command)
{
if (strcmp(cmd_command,"show")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"get")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"set")==0)
{
cmd_parse_value |= 2;
}
else if (strcmp(cmd_command,"system")==0)
{
cmd_parse_value |= 4;
}
else if (strcmp(cmd_command,"sys")==0)
{
cmd_parse_value |= 4;
}
else if (strcmp(cmd_command,"adc")==0)
{
cmd_parse_value |= 8;
}
else if (strcmp(cmd_command,"a2d")==0)
{
cmd_parse_value |= 8;
}
else if (strcmp(cmd_command,"channel1")==0)//
{
cmd_parse_value |= 16;
}
else if (strcmp(cmd_command,"ch1")==0)
{
cmd_parse_value |= 16;
}
}

最佳答案

由于您不在其他任何地方使用cmd_buffer_sprintf,因此该行代码似乎毫无用处。可能的情况是 cmd_buffer_sprintf 是您在其他地方使用的全局变量,但在您提供的代码段中,该行不会改变任何内容。

关于c - 为什么我需要这条线,据我所知它是多余的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942952/

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