[英]strtok() in c - segmentation fault
每当我尝试使用strtok()时,都会出现分段错误。 不知道为什么-我是C的新手。
这是我的代码:
#include "shellutils.h"
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char input[150];
while(1) {
prompt();
fgets(input, 150, stdin);
char *fst_tkn = strtok(input, " ");
printf("%s", fst_tkn);
if(feof(stdin) != 0 || input == NULL) {
printf("Auf Bald!\n");
exit(3);
}
}
}
谢谢你的帮助!
关于代码:
char *fst_tkn = strtok(input, " ");
printf("%s", fst_tkn);
如果input
变量为空或仅包含空格,则fst_tkn
将设置为NULL
。 然后,当您尝试将其打印为字符串时,所有选择均关闭。
您可以通过调整tou给input
的值在以下代码中看到这一点:
#include <stdio.h>
#include <string.h>
int main (void) {
char input[] = "";
char *fst_tkn = strtok (input, " ");
printf ("fst_tkn is %s\n", (fst_tkn == NULL) ? "<<null>>" : fst_tkn);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.