salut
je suis debutant en c et j'ai un problème avec mon code:
utilitaire.h
utilitaire.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 #ifndef UTILITAIRES_H #define UTILITAIRES_H typedef struct { int lignes; int colonnes; int** tb; }Tableau; Tableau *nouveau_tableau(int l,int c, int initialisation); void aff_tterrain(Tableau *tab, int lignes, int colonnes); #endif // UTILITAIRES_H
main.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 #include "utilitaires.h" Tableau *nouveau_tableau(int l,int c, int initialisation){ int i=0,j=0; Tableau *tab = NULL; tab->lignes = l; tab->colonnes = c; tab->tb=malloc(tab->lignes*sizeof(*tab->tb)); if(tab->tb == NULL) exit(0); for(i; i<tab->lignes; i++){ tab->tb[i]=malloc(tab->colonnes*sizeof(*tab->tb[i])); if(tab->tb[i]==NULL){ for(int p=i-1; p>=0; p--) free(tab->tb[p]); } } for(i; i<tab->lignes; i++){ for(j; j<tab->colonnes; j++){ tab->tb[i][j]=initialisation; } } for(i; i<tab->lignes; i++){ for(j; j<tab->colonnes; j++){ free(tab->tb[i][j]); } } return tab; } void aff_tterrain(Tableau *tab, int lignes, int colonnes){ for(int i = 0; i < tab->lignes; i++){ for(int j = 0; j < tab->colonnes; j++){ if(tab->tb[i][j] >= 10) printf(" %d |", tab->tb[i][j]); else printf(" %d |", tab->tb[i][j]); } printf("\n______________________________________________________\n"); } }
le truc c'est qu'au moment de la compilation il y'a plantage et je ne comprend pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 #include <stdio.h> #include <stdlib.h> #include "utilitaires.h" int main() { Tableau *carte = nouveau_tableau(10,5,0); aff_tterrain(carte,10,5); return 0; }
j'ai d'abord testé la fonction:
...qui marchait bien. mais je voudrais plutot fonctionner avec une structure que j'ai nommée "Tableau".
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 int **nouveau_tableau(int l,int c, int initialisation){ ... ... }
quelqu'un peut-il-m'aider?
merci d'avance
Partager