0
I’m making a program where I need to dynamically allocate two matrices, so I thought I’d make this allocation into a function. I tried to do it this way, but it’s giving "Segmentation Failure". Could someone help me?
void CriaMatriz(int ***matriz, int linhas, int colunas){
int i,j;
/* Alocação das linhas da matriz. */
matriz = (int ***) malloc(linhas * sizeof(int **));
/* Alocação das colunas da matriz. */
for (i = 0; i < linhas; i++) {
matriz[i] = (int **) malloc(colunas * sizeof(int*));
/* Preenchimento da matriz. */
for (j = 0; j < colunas; j++) {
scanf("%d", matriz[i][j]);
}
}
}
int main() {
int **matriz1, linhas, colunas;
CriaMatriz(&matriz1, linhas, colunas);
}
There are some conceptual errors in your code. The first is that you’ve allocated up to the maximum integer pointer level, which means that you could manipulate integer pointers, but not the integer itself. The other error I saw is that you are allocating a pointer pointer pointer, and assigning the result of that allocation to
matriz
, when you would like the function you calledCriaMatriz
had the first parameter with the allocation value, then you should allocate pointer and save the result of themalloc
in(*matriz)
– Jefferson Quesado