Je ne comprends pas pourquoi ce bout de code ne marche pas (Erreur de segmentation).
C'est la même chose que ce qu'il y a là mais en changeant char str[] en char *str. Je pensais que c'était la même chose, les pointeurs et les tableaux...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 char *str ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); }
Le code original :
En fait il faudrait que je fasse passer un pointeur vers une chaîne en paramètre (comme rvalue de str=...). Ça donne :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); }
Et là, c'est à peine mieux :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 char *str; strcpy(str,nomFichier); char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str," ,.-"); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, " ,.-"); }
Splitting string "../data/video/campus_raw.AVI" into tokens:
1
/data/video/campus_raw
2
AVI
Erreur de segmentation (core dumped)
Partager