Bonjour ,
Je declare une matrice sous forme pointeur de pointeur dans mon main.
Je veux allouer dynamiquement ma matrice dans une fonction incremente.
Seulement avec le code ci dessous , j'ai un segmentation fault situé dans la fonction affiche.
Je ne comprends pas d'ou viens l'erreur , si vous pouvez m'aiguiller.
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 #include <stdio.h> #include <stdlib.h> #include <string.h> void incremente (int** mat) { int i,j,k=0; mat=(int**)malloc(sizeof(int*)*4); for (i=0;i<4;i++) mat[i]=(int*)malloc(sizeof(int)*4); for (i=0;i<4;i++) for(j=0;j<4;j++) mat[i][j]=k++; } void affiche (int** mat) { int i,j; for (i=0;i<4;i++) { for(j=0;j<4;j++) printf("%d\t",mat[i][j]); printf("\n"); } } int main () { int** matrice; matrice=(int**)malloc(sizeof(int*)); incremente(matrice); affiche(matrice); return EXIT_SUCCESS; }
Merci de votre aide.
Partager