Bonjour,

cela fait bien 10 ans que je n'ai pas fait de C. Mais pour répondre à qq'un sur le forum, j'ai réécrit un petit bout de code.

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
#include <stdio.h>
#include <stdlib.h>
 
 
typedef struct _Matrice {
	int nbColonnes;
	int nbLignes;
	int **data;
} Matrice;
 
void showMatrice(Matrice *m) {
	int i,j,value;	
	int *ligne;
	printf(">> showMatrice\n");
	for (i=0;i<m->nbLignes;i++) {
		ligne = m->data[i];
		for (j=0;j<m->nbColonnes;j++) {
			value = *(ligne+j*sizeof(int));
			printf("%d ",value);
		}
		printf("\n");
	}
	printf("<< showMatrice\n");
}
 
Matrice * creerMatrice(int nbColonnes, int nbLignes) {
	Matrice * m = (Matrice *)malloc(sizeof(Matrice));
	m->nbColonnes = nbColonnes;
	m->nbLignes = nbLignes;
	m->data = (int **)malloc(nbLignes*sizeof(int *));
	int i,j;
	for (i=0;i<nbLignes;i++) {
	   m->data[i] =  (int*)malloc(nbColonnes*sizeof(int)); 
	   for (j=0;j<nbColonnes;j++)
	   	*(m->data[i]+j*sizeof(int)) = (int)7;   
	}	
	return m;
}
 
void libereMatrice(Matrice *m) {
	int i;
	for (i=0;i<m->nbLignes;i++) {
	   free(m->data[i]);	    	  
	}	
	free(m->data);
	free(m);
}
 
int main (int argc,char **argv)
{
	Matrice *m;	
 
	m = creerMatrice(2,2);
	showMatrice(m);
	libereMatrice(m);
 
 
	return 0;
}
Lors de l'exécution, les chiffres affichés en "fin" de tableau sont totalement farfelus.

Où est le hic ?

Merci d'avance