Pass array to functions in C

0

Good night, next I have a problem when passing an array to a function in C. I have the following code

void funcao(int mat[][num]);
int main() {
    scanf(%d,&num);
}
It is basically the following, I define the size of the array in the main function, so the statement of the function above the code points out that a is not a constant then it gives error, I wanted to know how I can this matrix for a function. Vlw

    
asked by anonymous 21.06.2017 / 01:19

2 answers

0

Try to put it like this:

void funcao(int **mat, int dim_linha, int dim_coluna);

Where mat is a multidimensional vector, and you pass the dimension of the array

    
24.06.2017 / 02:50
0

I suggest you use dynamic allocation:

#include <stdio.h>
#include <stdlib.h>

void funcao(int **mat, int num);
int main()
{
    int num;
    scanf("%d", &num);
    int **mat=(int**)malloc(num*sizeof(int*));
    for(int i=0; i<num; i++)
        mat[i]=(int*)malloc(num*sizeof(int));
    funcao(mat, num);
}
    
04.08.2017 / 18:08