Hello all, I've been trying to finish up a function that takes a string and breaks it down according to the whitespaces. It then saves it into a vector array (array of strings with a terminating NULL) and returns it. For some reason, when I attempt to change a whitespace into a '\0', I get a seg fault. I'm all out of ideas, so any help is much appreciated!
-It seg faults after encountering "*string = '\0'" on the first time.Code:char **my_str2vect(char* string) { char** vect; char* temp; int num_white = 0; temp = string; for(; *temp!= '\0'; temp++) { if(*temp == ' ') { num_white++; //Counts the number of whitespaces } } vect = (char**) xmalloc((num_white+1) *sizeof(char*)); //Allocates space for the char* array *vect = string; //Pointer to first string is saved at vect[0] for(; *string != '\0'; string++) { if(*string == ' ') { *string = '\0'; //This is where I get seg faults *vect = ++string; vect++; } } *vect = NULL; return vect; }
-xmalloc() is the same as malloc()