Recode strstr en C
evolution
-
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
KX Messages postés 16761 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour,
je dois recoder la fonction strstr en C, seulement j'ai un probleme (segmentation fault)
help,
char *my_strstr (char *str, char *to_find)
{
char *pointeur;
int i;
int j;
int counter;
pointeur = NULL;
i = 0;
j = 0;
counter = 0;
while (pointeur == NULL && str[i] != '\0')
{
if (str[i] == to_find[j])
{
while(str[j] == to_find[j])
{
j++;
if (to_find[j] == '\0')
*pointeur = to_find[i];
}
j = 0;
counter = i;
}
i++;
}
return (pointeur);
}
je dois recoder la fonction strstr en C, seulement j'ai un probleme (segmentation fault)
help,
char *my_strstr (char *str, char *to_find)
{
char *pointeur;
int i;
int j;
int counter;
pointeur = NULL;
i = 0;
j = 0;
counter = 0;
while (pointeur == NULL && str[i] != '\0')
{
if (str[i] == to_find[j])
{
while(str[j] == to_find[j])
{
j++;
if (to_find[j] == '\0')
*pointeur = to_find[i];
}
j = 0;
counter = i;
}
i++;
}
return (pointeur);
}
A voir également:
- My_strstr
- Xmedia recode - Télécharger - Conversion & Codecs
2 réponses
C'est ta ligne *pointeur = to_find[i]; qui plante.
pointeur vaut NULL donc *pointeur n'est pas accessible.
De plus to_find[i] est un caractère, pas un pointeur.
Si tu veux décaler le pointeur to_find de 'i' cases, tu peux faire to_find+i
En changeant ta ligne par pointeur = to_find+i; ça a l'air de marcher sur mon exemple my_strstr("bonjour","bon") mais il reste peut-être d'autres bugs...
pointeur vaut NULL donc *pointeur n'est pas accessible.
De plus to_find[i] est un caractère, pas un pointeur.
Si tu veux décaler le pointeur to_find de 'i' cases, tu peux faire to_find+i
En changeant ta ligne par pointeur = to_find+i; ça a l'air de marcher sur mon exemple my_strstr("bonjour","bon") mais il reste peut-être d'autres bugs...