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:
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]);
}
}
ecco l'esecuzione con tre token (e funziona correttamente):
codice:
# A primo | secondo | terzo |
0: 'primo '
1: ' secondo '
2: ' terzo '
ed ecco cosa succede se inserisco 4 o più token:
codice:
# A primo | secondo | terzo | quarto |
0: 'primo '
1: ' secondo '
Segmentation fault
qualche idea?
grazie in anticipo