1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//initialisation
char a[50] = {0};
char a1[50] = {0};
char a2[] = "hello, essai 1,toto,";
char* tab[10] = {0}; // tableau de chaine de caractère
char* tab1[10] = {0};
char* tab2[10] = {0};
char mot[10] = {0}; //chaine temporaire pour stocker un mot
char vide[10] = {0}; // je l'utilise pour vider ma chaine temporaire mot
int i,j = 0, nb_variables = 0;
/*
création de a, a1, a2
remplissage de tab et tab1, aucun problème, c'est exactement le même bout de code que pour tab2, c'est juste les chaines qui changent.
*/
//remplissage de tab2
for (i=0; i < strlen(a2); i++){
if (a2[i]!= ','){
mot[j] = a2[i];
}
else {
mot[j] = '\0';
if (mot != NULL){
tab2[nb_variables] = (char*) malloc(sizeof(mot));
strcpy(tab2[nb_variables],mot);
}
j = -1;
nb_variables++;
strcpy(mot,vide);
}
j++;
}
for(i=0; i < nb_variables; i++)
printf("tab2[%d] = %s\n", i, tab2[i]);
// erreur de segmentation, des caractères bizarres apparaissnt dans tab2[0]
return 0;
} |
Partager