chaine et comportement indefini
bonjour a tous.
je viens ici pour demander de l aide car j ai un comportement indefini qui je
pense viens de ma fonction str_to_wordtabtab. cette fonction a pour but de
transformer un buffer de plusieur chaine en un tableau contenant contenant
chaque mot inclu dans un tableau contenant chaque phrase.
J appelle donc la fonction str_to_wordtabtab a cet endroit:
Code:
1 2
|
make_cmd(args, str_to_wordtabtab(wash_string(buffer)), find_player(args, fd)); |
la fonction wash string a pour but de faire un premier ecremage de la
chaine en enlevant tout les \n et les \t en trop:
Code:
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
|
char *wash_string(char *str)
{
int cpt;
int i;
char *str2;
i = 0;
cpt = 0;
while (str[0] == ' ' || str[0] == '\t')
{
str++;
}
while (str[cpt] != '\0')
{
if (str[cpt] == ' ' || str[cpt] == '\t' || str[cpt] == '\n')
if (str[cpt + 1] == ' ' || str[cpt + 1] == '\t' || str[cpt + 1] == '\n')
i++;
cpt++;
}
str2 = xmalloc(cpt - i * sizeof(*str2));
cpt = 0;
i = 0;
while (str[i + cpt] != '\0')
{
while ((str[cpt + i] == ' ' || str[cpt + i] == '\t' || \
str[cpt + i] == '\n') && (str[cpt + 1 + i] == ' ' \
|| str[cpt + 1 + i] == '\t'))
cpt++;
str2[i] = str[cpt + i];
i++;
}
if (str2[i - 2] == ' ' && str2[i - 1] == '\n')
{
str2[i - 2] = '\n';
str2[i - 1] = '\0';
}
str2[i] = '\0';
return (str2);
} |
mon str_to_word_tabtab utilise 3 fonction que j ai coder pour me faciliter la
tache:
my_nbstr qui me renvoie le nombre d occurence d un caractere dans une chaine.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int my_nbstr(char *str, char c)
{
int i;
int cpt;
i = 0;
cpt = 0;
while(str[cpt] != '\0')
{
if (str[cpt] == c)
i++;
cpt++;
}
return (i);
} |
my_inbstr qui me renvoie la position d une occurence dans une chaine.
Code:
1 2 3 4 5 6 7 8 9 10
|
int my_inbstr(char *str, char c)
{
int i;
i = 0;
while(str[i] != c && str[i] != '\0')
i++;
return (i);
} |
my_strchr qui m avance mon pointeur sur chaine jusqu a trouver une
occurence
Code:
1 2 3 4 5 6 7 8 9
|
void my_strchr(char **str, char c)
{
while (*(*str) != c && *(*str) != '\0')
(*str)++;
if (*(*str) == c)
(*str)++;
return ;
} |
et voici mon str_to_wordtabtab
Code:
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
|
char ***str_to_wordtabtab(char *str)
{
int i;
int j;
char ***tab;
char *tmp;
char *tmp2;
char *str2;
tab = xmalloc((my_nbstr(str,'\n') + 2) * sizeof(*tab));
tab[my_nbstr(str,'\n')] = '\0';
str2 = str;
for (i = 0 ; i < my_nbstr(str, '\n'); i++)
{
tmp = xmalloc((my_inbstr(str2, '\n') + 2) * sizeof(*tmp));
memcpy(tmp, str2, my_inbstr(str2, '\n'));
tmp[my_inbstr(str2, '\n')] = '\0';
tab[i] = xmalloc((my_nbstr(tmp, ' ') + 2) * sizeof (**tab));
tmp2 = tmp;
for (j = 0 ; j < my_nbstr(tmp2, ' ') + 1; j++)
{
tab[i][j] = xmalloc((my_inbstr(tmp,' ') + 2) * sizeof(***tab));
memcpy(tab[i][j], tmp, my_inbstr(tmp,' '));
tab[i][j][my_inbstr(tmp, ' ')] = '\0';
my_strchr(&tmp, ' ');
}
tab[i][j] = '\0';
my_strchr(&str2, '\n');
tmp = tmp2;
free(tmp);
}
tab[i] = '\0';
free(str);
return (tab);
} |
je pense que mon probleme vient ou du positionnement des '\0' ou du
mallocage car il me cree des erreurs d affichage ou de temp en temp de segmentation fault, mais je n arrive pas a voir l erreur que je fais.
merci de votre aide :mrgreen: