I'm trying to split user input into an array of strings in C. It works 40% of the time. Sometimes, users will input a string and all will be as predicted. Sometimes, the same string will be inputted, and the content of the arrays will be different. I cannot figure out why.
void run_game(struct board *made_board){
char **user_command;
user_command = (char **)malloc(sizeof(char *)*4);
user_command = split_string(user_command);
printf("Input1: %s\n", user_command[0]);
printf("Input2: %s\n", user_command[1]);
printf("Input3: %s\n", user_command[2]);
}
char** split_string(char **user_input){
char user_command[10];
char *word;
int word_counter = 0;
fgets(user_command, 10, stdin);
while(strlen(user_command)>9){
fgets(user_command, 10, stdin);
}
word = strtok (user_command," ");
user_input[word_counter] = word;
word_counter++;
while (word != NULL){
word = strtok (NULL, " ");
user_input[word_counter] = word;
word_counter++;
}
return user_input;
}
Input1, Input2 and Input3 sometimes will be what comes in from fgets, sometimes it will be random characters. That's two different behaviour for the exact same input.