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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| /* -ed- manquants. */
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* -ed- fonctions non exportees. Ajoute 'static' */
static 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)
{
/* -ed- on a aucune garantie que cette taille soit suffisante */
char buf[BUFSIZ];
strcpy(buf, *p1);
strcpy(*p1, *p2);
strcpy(*p2, buf);
}
}
static void print(FILE *f, char **tab)
{
for ( ; *tab != NULL; ++tab)
fprintf(f, "%s\n", *tab);
}
static int cmp_str (void *a, void *b)
{
return strcmp (a, b);
}
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);
/* -ed-
tri(t,&strcmp));
une parenthese en trop...
un & en trop.
strcmp n'a pas le bon stype. Il faut une fonction 'relais'.
*/
tri(t, cmp_str);
print(stdout, t);
return EXIT_SUCCESS;
} |
Partager