gpt4 book ai didi

c - 解析器代码中的奇怪编译器错误

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

解析器.h

enum { PLUS, MINUS, DIVIDE, MULTIPLY, NUMBER, END } type;

int token;

/* parsing functions */
void parse_token (void);

解析器.c

void get_token (void)
{
token++;
parse_token(); /* LINE 11 */
}

void parse_token (void) /* LINE 14 */
{
if ( strchr ("1234567890.", token) )
type = NUMBER;

else if ( strchr ("+", token) )
type = PLUS;

else if ( strchr ("-", token) )
type = MINUS;

else if ( strchr ("/", token) )
type = DIVIDE;

else if ( strchr ("*",token) )
type = MULTIPLY;

else if ( token == '\0' )
type = END;
else
show_error(strcat("Couldn't parse token : ", token));
}

错误

parser.c:14:6: warning: conflicting types for ‘parse_token’ [enabled by default]
parser.c:11:2: note: previous implicit declaration of ‘parse_token’ was here
parser.c: In function ‘parse_token’:
parser.c:16:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:17:3: error: ‘type’ undeclared (first use in this function)
parser.c:17:3: note: each undeclared identifier is reported only once for each function it appears in
parser.c:17:10: error: ‘NUMBER’ undeclared (first use in this function)
parser.c:19:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:20:10: error: ‘PLUS’ undeclared (first use in this function)
parser.c:22:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:23:10: error: ‘MINUS’ undeclared (first use in this function)
parser.c:25:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:26:10: error: ‘DIVIDE’ undeclared (first use in this function)
parser.c:28:2: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [enabled by default]
/usr/include/string.h:235:14: note: expected ‘int’ but argument is of type ‘char *’
parser.c:29:10: error: ‘MULTIPLY’ undeclared (first use in this function)
parser.c:32:10: error: ‘END’ undeclared (first use in this function)
parser.c: In function ‘show_error’:
parser.c:40:2: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]

我完全被蒙蔽了。 :(.

有什么帮助吗?

最佳答案

一旦你让它编译(通过包含 header ,正如 Luchian Grigore 所说),你会发现你不能对常量字符串执行 strcat()

常量字符串分配在只读内存中,不能修改。即使您可以修改它,您也会覆盖内存中的其他内容。

关于c - 解析器代码中的奇怪编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8756342/

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