Re: erreur de segmentation
Citation:
Envoyé par Man_Utd
en compilant ce code ,j'ai un seg fault mais je ne vois pas d'erreur.
Si ce que tu dis est vrai, c'est que ton compilateur est sérieusement buggé ! Mais je suppose que tu veux dire
"en exécutant ce code ,j'ai un seg fault mais je ne vois pas d'erreur."
ceci dit, pour le moment, il ne compile pas.
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 41 42 43
|
Compiling: main.c
main.c:2: warning: no previous prototype for 'tri'
main.c: In function `tri':
main.c:5: error: `NULL' undeclared (first use in this function)
main.c:5: error: (Each undeclared identifier is reported only once
main.c:5: error: for each function it appears in.)
main.c:9: error: `BUFSIZ' undeclared (first use in this function)
main.c:11: error: implicit declaration of function `strcpy'
main.c:11: warning: nested extern declaration of `strcpy'
<internal>:0: warning: redundant redeclaration of 'strcpy'
main.c:9: warning: unused variable `buf'
main.c: At top level:
main.c:20: error: syntax error before '*' token
main.c:21: warning: function declaration isn't a prototype
main.c: In function `print':
main.c:22: error: `tab' undeclared (first use in this function)
main.c:22: error: `NULL' undeclared (first use in this function)
main.c:23: error: implicit declaration of function `fprintf'
main.c:23: warning: nested extern declaration of `fprintf'
<internal>:0: warning: redundant redeclaration of 'fprintf'
main.c:23: error: `f' undeclared (first use in this function)
main.c: In function `main':
main.c:30: error: implicit declaration of function `malloc'
main.c:30: warning: nested extern declaration of `malloc'
<internal>:0: warning: redundant redeclaration of 'malloc'
main.c:31: error: `NULL' undeclared (first use in this function)
main.c:33: warning: nested extern declaration of `fprintf'
<internal>:0: warning: redundant redeclaration of 'fprintf'
main.c:33: error: `stderr' undeclared (first use in this function)
main.c:34: error: `EXIT_FAILURE' undeclared (first use in this function)
main.c:43: error: implicit declaration of function `strlen'
main.c:43: warning: nested extern declaration of `strlen'
<internal>:0: warning: redundant redeclaration of 'strlen'
main.c:46: warning: nested extern declaration of `fprintf'
<internal>:0: warning: redundant redeclaration of 'fprintf'
main.c:50: warning: nested extern declaration of `strcpy'
<internal>:0: warning: redundant redeclaration of 'strcpy'
main.c:55: error: `stdout' undeclared (first use in this function)
main.c:56: error: `strcmp' undeclared (first use in this function)
main.c:56: error: syntax error before ')' token
main.c:59: error: `EXIT_SUCCESS' undeclared (first use in this function)
Process terminated with status 1 (0 minutes, 1 seconds) |
Ce serait bien de poster du code complet si tu veux qu'on vérifie dans les mêmes conditions que toi... Ca parait évident, non ?
Ceci fonctionne avec la ligne de commande suivante :
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 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;
} |
Code:
1 2 3 4 5 6 7 8 9
|
789
456
123
123
456
789
Press ENTER to continue. |