Bonjour,
Je souhaite allouer dynamiquement un tableau à deux dimensions.
Du coup j'ai d'abord créé une fonction qui prend en paramètre une hauteur et une largeur et qui renvoie un tableau à deux dimensions dont voici le code :
Et je l'appel dans le main :
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 float ** allocation( int height, int width ) { int i,j; float ** tab = (float *) malloc( height*sizeof(float*) ); /*Ligne 153*/ for ( i = 0; i<height; i++ ) { tab[i] = (float* ) malloc( width*sizeof(float) ); for ( j = 0; j<width; j++ ) { tab[i][j] = 1; } } return tab; }
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 int main(void){ /*....code déterminant height et width....*/ float *imageVect=NULL; float **tab2=NULL; float **matrice=NULL; imageVect = (float *) malloc(height*width*nbImage * sizeof(float)); /*Appel des fonctions*/ tab2=allocation(height,width); imageVect=LireImageRAW(height,width,nbImage); matrice=DecompVect(imageVect,tab2,height,width,positionOffset); /*Ligne 240*/ }
Le but de mon programme est de transcrire un vecteur en une matrice en 512*512
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 float* DecompVect(float* imageVect,float* tab2,int height,int width,int positionOffset){ /*Declaration des variables*/ int i,j; /*fin de déclaration des variables*/ for(i=0;i<height;i++){ for(j=0;j<width;j++){ tab2[i][j]=imageVect[positionOffset]; /*Ligne 179*/ printf("%.2f\n",tab2[i][j]); positionOffset++; } printf("\n"); } return(tab2); free(tab2); }
Mon problème est que lorsque je compile le code voici l'erreur que j'ai :
error: cannot convert 'float*' to 'float**' in initialization => ligne 153
error : invalid types 'float[int]' for array subscript => ligne 179
error: cannot convert 'float**' to 'float*' for argument '2' to 'float* DecompVect((float*,float*,int,int,int)' => ligne 240
Je pense que mes erreurs sont liées à une seule et même erreur (lors de l'allocation du tableau je pense), mais je n'arrive pas à trouver la faute !
Merci de vos réponses !
Partager