# Thread: Help with passing multidimensional array to function by ref.

1. ## Help with passing multidimensional array to function by ref.

Hey

Here is code one:
Code:
```int main() {
int i, j;
int **matriz;
// alocando espa&#231;o para as colunas
matriz = (int **)malloc(N*sizeof(int *));
// alocando espa&#231;o para as linhas, uma de cada vez
for (i=0;i<N;i++)
matriz[i] = (int *)malloc(N*sizeof(int ));
// inserindo n&#250;meros na matriz
for (j=0;j<N;j++)
for (i=0;i<N;i++)
matriz[i][j] = 0;
}```
Now what i am trying to do is this:

Code:
```void criaMatriz(int** *matriz, int N);

void criaMatriz(int** *matriz, int N) {
int i, j;
// alocando espa&#231;o para as colunas
*matriz = (int **) malloc(N*sizeof(int *));
// alocando espa&#231;o para as linhas, uma de cada vez
for (i=0;i<N;i++)
*(matriz[i]) = (int *) malloc(N*sizeof(int));
// inserindo n&#250;meros na matriz
for (j=0;j<N;j++)
for (i=0;i<N;i++)
matriz[i][j] = 0;
}
int main {
...
criaMatriz(&matriz, N);
}```
But i'm getting segfaults. Clearly i'm passing it (&matriz) the wrong way, or i'm making mistakes inside the function. the error occurs in the line
Code:
`*(matriz[i]) = (int *) malloc(N*sizeof(int));`
Any help is appreciated

2. > *(matriz[i]) = (int *) malloc(N*sizeof(int));
1. Don't cast malloc in a C program, see the FAQ.

2. You have to be really careful with the dereferencing and precedence here.
This should be
(*matriz)[i] = malloc(N*sizeof(int));

Likewise, later on
(*matriz)[i][j] = 0;

The initial call is fine.

3. Uh.. dull sintax errors

I will take more attention next time

Thank you very much!