Bonjour,
J'ai un gros problème avec un pointeur !
Je devellope une fonction qui recherche un mot dans un texte afin de
stocker l'indice de début et de celui-ci. Pour ce stockage j'utilise un
pointeur a deux dimensions. Malheuresement, j'ai l'impression que ce
pointeur s'efface une fois que je sors de la fonction !
Si vous pouviez regarder mon code s'il vous plait...
Merci beaucoup
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int rechercher(int **selection,char *tab, int cpt_selection, char *mot, int mode,int *cpt)
{
int cpt2;
int tai_mot=strlen(mot);
while (tab[*cpt]!=0)
{
cpt2 = 0;
if (tab[*cpt]==mot[cpt2])
{
while ((tab[*cpt]==mot[cpt2])&&(cpt2<=tai_mot))
{
(*cpt)++;
cpt2++;
}
if
(cpt2==tai_mot) //on a trouvé un mot. On sauvegarde donc l'indice du
début et de fin de ce mot dans le tableau de selection
{
selection=(int **) realloc(selection , (cpt_selection+1) * sizeof(int
*));//on alloue le nombre de ligne nécessaire
*(selection+cpt_selection)=(int *) malloc(2 * sizeof(int)); //on alloue
les deux cases
selection[cpt_selection][0]=*cpt-tai_mot;
selection[cpt_selection][1]=*cpt-1;
cpt_selection++;
}
}else
{
(*cpt)++;
}
}
printf("\ndebut : %ld\n",selection[0][0]);
printf("fin : %ld\n",selection[0][1]);
return cpt_selection;
}
int main()
{
char tab[300]={"C'est un point important ! point point "};
char mot[10]={"point"};
int **selection;
int tai_util=strlen(tab);
int cpt_selection=0, mode=0, cpt=0;
selection=(int **) malloc(1 * sizeof(int *)); //on alloue de l'espace pour le tableau de selection
puts(tab);
printf("\n\n");
printf("2 : rechercher tout");
fflush(stdin);
scanf("%ld",&mode);
if (mode==2) cpt_selection=rechercher(selection,tab,cpt_selection,mot,mode,&cpt);
printf("\ncpt selection : %ld\n",cpt_selection);
printf("debut après : %ld\n",selection[0][0]);
printf("fin après : %ld\n",selection[0][1]);
system("PAUSE");
return 0;
}