Bonsoir,
Pouvez vous m'aider à trouver mon erreur ?
voila mon code :
Mon liste.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "liste.h" #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <ctype.h> #include <string.h> struct skieurs { int matricule; char * nom; char * prenom; }; void main( void ) { struct liste * l; struct skieurs * s; int i = 0; char * mat = NULL; while ( i < 3 ) { s = ( struct skieurs * ) malloc( sizeof( struct skieurs ) ); printf( " entrez matricule \n" ); fflush( stdin ); fgets( mat, sizeof mat, stdin ); s->matricule = atoi( mat ); // ou strtoi(mat); printf( " entrez nom du skieur : \n" ); fflush( stdin ); fgets( s->nom, sizeof s->nom, stdin ); printf( " entrez prenom du skieur : \n" ); fflush( stdin ); fgets( s->prenom, sizeof s->prenom, stdin ); add_itemInEnd( l, s ); free( s ); i++; } getch(); free( l ); l->courant = l->depart->suivant; while ( l ) { s = ( struct skieurs * ) l->courant->data; printf( " %d -- %s -- %s \n", s->matricule, s->nom, s->prenom ); free( s ); } getch(); } /* fin prog */
Et le liste.h (au cas où )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 #ifndef _liste_c_ #define _liste_c_ #include <stdlib.h> #include <stdio.h> // Déclaration de mes structures de données //************************************************************************ typedef struct cellule { void * data; //pointeur sur structure struct cellule * precedent; struct cellule * suivant; }; typedef struct liste { struct cellule * depart; struct cellule * courant; struct cellule * fin; int size; }; //Initialisation de la liste //************************************************************************** void initList( struct liste * l ) { l->depart = ( struct cellule * ) malloc( sizeof( struct cellule ) ); l->fin = ( struct cellule * ) malloc( sizeof( struct cellule ) ); l->fin->precedent = l->depart; l->depart->suivant = l->fin; l->fin->suivant = NULL; l->depart->precedent = NULL; l->size= 0; } // Ajout d'un élement dans la liste //*************************************************************************** void add_itemInEnd( struct liste * l, void * data ) { l->courant = l->depart->suivant; while ( l ) { if ( l->courant->suivant == NULL ) { l->courant->suivant = ( struct cellule * ) malloc( sizeof( struct cellule ) ); l->courant->precedent->suivant = l->courant; l->courant->suivant->precedent = l->courant; l->courant = l->courant->suivant; l->courant->data = data; l->fin=l->courant; l->fin->precedent=l->courant->precedent; l->courant->suivant = NULL; } else { l->courant = l->courant->suivant; } } } // Suppression d'un élement dans la liste //***************************************************************************** void delete_item( struct liste * l, void * data ) { int * element, * inserted; int deleted = 0; l->courant = l->depart->suivant; while ( l ) { inserted = ( int * ) data; element = ( int * ) l->courant->data; if ( * inserted == * element ) { l->courant->precedent->suivant = l->courant->suivant; l->courant->suivant->precedent = l->courant->precedent; free( l->courant ); deleted = 1; } else { l->courant = l->courant->suivant; } } if ( deleted == 1 ) { printf( " Item was deleted successfully\n" ); getchar(); } else { printf( " Item not found \n" ); getchar(); } } // Vide la liste //****************************************************************************** int empty_list( struct liste * l ) { return ( l->depart->suivant == NULL && l->fin->precedent == NULL ); } #endif //***************************************************************************
Mon erreur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #ifndef _liste_h_ #define _liste_h_ struct cellule {void *data;struct cellule *precedent;struct cellule *suivant;}; struct liste {struct cellule *depart;struct cellule *courant;struct cellule *fin ;}; void initList(struct liste *l); void add_itemInEnd( struct liste * l, void * data ); void delete_item( struct liste * l, void * data ); int empty_list( struct liste * l ); #endif
***********
Sur C - builerxau runtime, il me laisse encoder le 1er matricule , ensuite erreur de type : Access Violation .
Et sur Turbo Cnull pointer assignement . Il encode 2 skieurs ensuite plus rien ! excepté -> Null pointer Exception .
A la compilation , il n'y a aucune erreur .
P.S : j'ai poster tous le code du <liste.c> mais ici , on ne s'intéresse qu'à la fonction "add_itemInEnd " ...
Merci pour votre aide .
Partager