【发布时间】:2015-11-29 17:03:54
【问题描述】:
运行程序后我不断收到错误消息。 错误是“分段错误(核心转储)”,我收到了一条注释。
note: expected ‘const char *’ but argument is of type ‘char’
extern int strcmp (const char *__s1, const char *__s2)
这是我的代码:
int main()
{
char cmdline[100];
printf ("esp> ");
fgets( cmdline, sizeof( cmdline ), stdin );
char *args[4] = { NULL };
char *pchcaus;
pchcaus = strtok (cmdline," ");
int i = 0;
while (pchcaus != NULL)
{
args[i++] = pchcaus;
pchcaus = strtok (NULL, " ");
}
char* command = args[0];
char* argumentOne = args[1];
char* argumentTwo = args[2];
char* input = { "quit" };
printf ("%s", command); // this prints "quit"
if (strcmp(command, input) == 0) { // this is not working. why ?
printf("Bye.\n" );
}
return 0;
}
如果我输入 quit 它会返回“分段错误(核心转储)”。其他一切都在工作,但最后一个 if 语句。比较字符串的那个
【问题讨论】:
-
要么你得到一个编译器错误(
strcmp注释是关于)要么你得到一个崩溃(分段错误)。您不能真正从同一个程序中获得两者,因为该程序需要在没有错误的情况下构建,您才能运行它并导致崩溃。 -
据我所知,您尝试将
(char**) {"quit"}隐式转换为char* input。尝试删除那些花括号,它应该是char*? -
@JoachimPileborg 我怀疑这只是一个编译器警告(如果这是我怀疑的错误,
gcc应该抛出,请参阅上面的评论) -
cmdline未定义。没有main()功能。请提供您实际编译的代码,而不是它的子集。 -
尝试将
char* input = { "quit" };替换为char input[] = "quit";。 `.