我正在尝试读取用户的输入,然后标记每个单词并将每个单词放入字符串数组中。最后,打印出数组的内容以供调试。我的代码如下。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int MAX_INPUT_SIZE = 200;
volatile int running = 1;
while(running) {
char input[MAX_INPUT_SIZE];
char tokens[100];
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
//tokenize input string, put each token into an array
char *space;
space = strtok(input, " ");
tokens[0] = space;
int i = 1;
while (space != NULL) {
space = strtok(NULL, " ");
tokens[i] = space;
++i;
}
for(i = 0; tokens[i] != NULL; i++) {
printf(tokens[i]);
printf("\n");
}
printf("\n"); //clear any extra spaces
//return (EXIT_SUCCESS);
}
}
在“shell>”提示符下输入我的输入后,gcc 给了我以下错误:
Segmentation fault (core dumped)
关于为什么会发生此错误的任何想法?在此先感谢您的帮助!