Segmentation fault (core dumped) issue
I am trying to read in input from a user, and then tokenize each word and
put each word into an array of strings. At the end, the contents of the
array are printed out for debugging. My code is below.
#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);
}
}
After I type in my input at the "shell> " prompt, gcc gives me the
following error:
Segmentation fault (core dumped)
Any idea as to why this error is happening? Thanks in advance for your help!
No comments:
Post a Comment