Ciao a tutti, sto avendo problemi a realizzare una funzione che, data in input una stringa e un carattere di escape, mi esegue la "tokenizzazione" della stringa e mi inserisca ogni token all'interno di un array di stringhe che verrà restituito in output.
ecco il codice:
ecco l'esecuzione con tre token (e funziona correttamente):codice:char **token(char *stringa, char *escape, int *count){ char **out = NULL, *str; int fine = 1, i=0, j; out = (char **)malloc(sizeof(char **)); str = strtok(stringa, escape); out[i] = (char *)malloc((strlen(str)+1)*sizeof(char *)); strcpy(out[i],str); while(fine){ str = strtok(NULL, escape); if(str != NULL){ out = (char **)realloc(out, ++i*sizeof(char **)); out[i] = (char *)malloc((strlen(str)+1)*sizeof(char *)); strcpy(out[i],str); }else{ fine = 0; } } *count = i+1; for(j=0; j<*count; j++){ printf("\n%d: '%s'",j,out[j]); } }
ed ecco cosa succede se inserisco 4 o più token:codice:# A primo | secondo | terzo | 0: 'primo ' 1: ' secondo ' 2: ' terzo '
qualche idea?codice:# A primo | secondo | terzo | quarto | 0: 'primo ' 1: ' secondo ' Segmentation fault
grazie in anticipo

Rispondi quotando