Gattuso | Bonjour,
en compilant ce code ,j'ai un seg fault mais je ne vois pas d'erreur.
Est ce que vous pourriez m'aider.
Merci
Code :
- void tri(char **tab, int(*cmp)(void *, void *))
- {
- char **p1,**p2;
-
- for(p1=tab; *p1!=NULL; p1++)
- for(p2=p1+1; *p2!= NULL; p2++)
- if((*cmp)(*p1,*p2) > 0)
- {
- char buf[BUFSIZ];
-
- strcpy(buf,*p1);
-
- strcpy(*p1,*p2);
- strcpy(*p2,buf);
- }
- }
- void print(FILE *f, char **tab)
- {
- for( ; *tab!=NULL; ++tab)
- fprintf(f,"%s\n",*tab);
- }
- int main(int argc, char *argv[])
- {
- char **t = malloc(sizeof(char *)*argc);
- if(t == NULL)
- {
- fprintf(stderr,"erreur \n" );
- return EXIT_FAILURE;
- }
-
- unsigned int l,i;
-
- l = (unsigned int) argc-1U;
-
- for(i = 0; i<l; i++)
- {
- t[i] = malloc(strlen(argv[i+1])+1);
- if(t[i] == NULL)
- {
- fprintf(stderr,"erreur \n" );
- return EXIT_FAILURE;
- }
-
- strcpy(t[i],argv[i+1]);
-
- }
-
- t[l] = NULL;
- print(stdout,t);
- tri(t,&strcmp));
- print(stdout,t);
- return EXIT_SUCCESS;
- }
|
|