hello. i am trying to make a split function that takes in a string and a delimiter, and returns a array of strings that have been separated by the delimiter.
here is my code so far:
the input i used is: "this, is, crazy"Code:char * split(char string[], char * sep){ // char str[] = &string; char * pch; char * out[3]; int i=0; printf ("h1\n"); pch = strtok (string," ,"); printf ("h2\n"); while (pch != NULL) { printf ("\"%s\"\n",pch); out[i] = pch; i++; if(pch != NULL) pch = strtok (NULL, " ,"); } //out[i] = '\0'; printf("h3 %s\n",out[0]); return out; } int main(){ printf("start\n"); char * out = split(str, " ,"); printf("end\n"); printf ("out 1: %s\n",out[0]); // segmentation fault happens here printf ("out 2: %s\n",out[1]); printf ("out 3: %s\n",out[2]); return 0; }
i get a segmentation fault after the printf of "end"



LinkBack URL
About LinkBacks


