I'm trying to pass a pointer to a pointer to a procedure, but i get a segfault everytime, while passing it to a function works perfectly fine. I guess it may have something to do with the coercion C does automatically on arrays to pointers, such as in this question : passing a pointer to a pointer in C .
But i don't know how to fix it.
Here's the code :
#include <stdio.h>
#include <stdlib.h>
void creer_matrice(int nbCol, int nbLigne, char **matrice)
{
int i,j;
matrice = calloc( nbCol, sizeof(char*));
if( matrice == NULL ){
printf("Allocation impossible");
exit(EXIT_FAILURE);
}
for( i = 0 ; i < nbLigne ; i++ ){
matrice[i] = calloc (nbCol, sizeof(char*));
if( matrice[i] == NULL ){
printf("Allocation impossible");
exit(EXIT_FAILURE);
}
}
/* Remplissage de test*/
for(i = 0; i < nbLigne; i++){
for(j = 0; j < nbCol; j++){
matrice[i][j] = 'I';
}
}
//return matrice;
}
int main(){
int i,j;
char **matrice;
creer_matrice(8,6,matrice);
//matrice = creer_matrice(8,6);
for(i = 0; i < 6; i++){
for(j = 0; j < 8; j++){
printf("%c ",matrice[i][j]);
}
printf("\n");
}
}
Can somebody please tell me where i am wrong and how to solve it ?