I have a segmentation fault and would like to know where is my mistake.
Let me explain.
In my main, I declare a 3D array: int*** Matricegroupegeneralisant
Then this main uses a function recuperationinfoFich(&matricegroupegeneralisant);
This function is declared as : recuperationinfoFich(int* * * * matricegroupegeneralisant)
This function recuperationinfoFich uses another function recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[Ni]);
This function is declared as recuperationmatricegroupesgeneralisants( int*** matricegroupegeneralisant)
My code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void allocationdynamiquetableautroisdimdentier(int**** Matrice,int nbniveau, int nbligne, int nbcolonne)
{
int i,j;
*Matrice=(int***) malloc (sizeof(int**)*nbniveau);
for (i=0; i<nbniveau; i++)
{
(*(Matrice))[i]=(int**) malloc (sizeof(int*)*nbligne); // allocation dynamique de la matrice Matrice
for (j=0; j<nbligne; j++)
{
((*(Matrice))[i])[j]=(int*) malloc (sizeof(int)*nbcolonne);
}
}
}
void recuperationmatricegroupesgeneralisants(int*** matricegroupegeneralisantA)
{
(*matricegroupegeneralisantA)[0][1]=1;
}
void recuperationinfoFich(int**** matricegroupegeneralisantA)
{
allocationdynamiquetableautroisdimdentier(matricegroupegeneralisantA,3, 3, 7);
recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[1]);
}
void main(int args, char **argv)
{
int*** matricegroupegeneralisantA;
recuperationinfoFich(&matricegroupegeneralisantA);
}
With Gdb :
(gdb) r
Starting program: /home/larimsna1/Desktop/a.out
Breakpoint 1, 0x000000000040061a in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.
Program received signal SIGSEGV, Segmentation fault.
0x00000000004005c8 in recuperationmatricegroupesgeneralisants ()
(gdb)
I suspect the problem has something to do with your allocation function. That being said, there are numerous functional and stylistic issues with the code you provided. In one place, you failed to dereference a pointer enough times, causing you to assign an integer to a pointer type. You needlessly use pointers for most of your transactions when you could simply not pass an argument and use a return value.
This code should do what you want, and should work properly, and is easier to read:
Also, from a stylistic perspective, your variable names are ridiculously long, and you should use spaces around operators.
A lot of the mistakes in this code are mistakes the compiler is capable of detecting and warning you about. They are valid C code that will compile properly, but in this and most other cases are written in error and will not work as intended. You should compile with compiler warnings to weed out possible accidents: