I was given the parseInput function from a teacher. I am not able to get it to run without a segmentation fault and I hope you guys can help me.
I think it has to do with how I pass total_cmd_words, but I'm not sure.
// Libraries
#include <stdio.h>
#include <string.h>
int parseInput(char *input, char* splitWords[]){
int wordInd = 0;
splitWords[0] = strtok(input, " "); // VSCODE says this line is causing the fault
while(splitWords[wordInd] != NULL){
splitWords[++wordInd] = strtok(NULL, " ");
}
return wordInd;
}
int main(){
char* total_cmd = "file1 > file2";
char* total_cmd_words[] = {};
parseInput(total_cmd, total_cmd_words);
}
gdb gives this output:
__GI___strtok_r (s=0x555555556006 "file1 > file2", delim=0x555555556004 " ",
save_ptr=0x7ffff7fb0ca8 <olds>) at strtok_r.c:72
72 strtok_r.c: No such file or directory.
Changing:
char* total_cmd_words[] = {};
to this:
char* total_cmd_words[100] = {};
Still results in a segmentation fault.