I am new in C and all this thing of strings is pretty confusing.
The objective of this part of the program is to split a string into multiple strings, in this way I can process the words separately.
int SplitString(char * str, char * pieces[]) {
int i=1;
if ((pieces[0]=strtok(str," \n\t"))==NULL){
return 0;
}
while ((pieces[i]=strtok(str," \n\t"))!=NULL) {
i++;
}
return i;
}
/*****************************************************************************/
void CommandPros(char *str) {
char *pieces[100];
int numW = 0;
for (int i = 0; i < 100; i++) {
pieces[i] = (char *) malloc (100*sizeof(char));
}
numW = SplitString(str, pieces);
}
/*****************************************************************************/
void ReadEnter(char * str) {
fgets(str, 100, stdin);
}
/*****************************************************************************/
int main(){
int fin = 0;
char * strCommand;
strCommand = (char *) malloc (100*sizeof(char));
while (!fin) {
ReadEnter(strCommand);
CommandPros(strCommand);
}
return 0;
}
But when I execute the program this message appear:
Segmentation fault: 11
whileloop terminate?PrintPrompt? And please provide a sample input that triggrs the promlem. Read this: How to Ask and this: minimal reproducible example