Bonjour,
J'aimerais comprendre pourquoi ce code fonctionne:
Et celui ci me sort une erreur de segmentation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 int main(int argc, char ** argv){ int n = 0; size_t taille = 0; char *c1; char *c2; char *saveptr; while(getline(&c1, &taille, stdin) != -1){ printf("Ligne %d\n", ++n); c2 = strtok_r(c1, "\t ", &saveptr); while(c2 != NULL){ printf("\t%s\n", c2); c2 = strtok_r(NULL, "\t ", &saveptr); } } printf("\nDone\n"); return 0; }
Sachant que le prototype de strtok_r est:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 int main(int argc, char ** argv){ int n = 0; size_t taille = 0; char *c1; char *c2; char **saveptr; while(getline(&c1, &taille, stdin) != -1){ printf("Ligne %d\n", ++n); c2 = strtok_r(c1, "\t ", saveptr); while(c2 != NULL){ printf("\t%s\n", c2); c2 = strtok_r(NULL, "\t ", saveptr); } } printf("\nDone\n"); return 0; }
Je croyais que un pointeur1 de pointeur2 contenais l'adresse de pointeur2, equivalent a &pointeur2.char *strtok_r(char *str, const char *delim, char **saveptr);
Partager