Hey there,
I'm having issues with a custom string tokenizer I'm using for an assignment. I've looked around and haven't managed to find anything that really answers my question, so here goes nothing.
Whenever I run the program and it runs the tokenizer, I get a seg fault. I'm fairly new to pointer arithmetic, so if someone could push me in the right direction, that would be great. Here is the offending function and main function:
char** tokenize(char* str, char ch, char** arr){
while(*str != '\0'){ /*While we have not reached the end of the string*/
if(*str == ch){
str++; /*Increment str*/
arr++; /*Increment arr*/
}
**arr = *str; /*The character at arr's current string = the character at str.*/
*arr++; /*Next character in arr's string*/
str++; /*Next memory location in str.*/
}
return arr;
}
int main(int argc, char *argv[]){
char* input = (char*)malloc(sizeof(char) * MAXLINE);
char** buf = (char**)malloc(sizeof(char*) * (MAXLINE/2));
int i = 0;
for(;;){
printf("sedit>> ");
fgets(input,MAXLINE,stdin);
buf = tokenize(input,' ',buf);
printf("%s",*buf);
printf("%s",input);
printf("\n");
}
return 0;
}
Thank you kindly!