Pass matrix to C functions

Asked

Viewed 879 times

0

Good evening, next I have a problem passing a matrix to a C function. 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 matrix in the main function, so the declaration of the function above the code points out that one is not a constant so it gives error, I wanted to know how I can this matrix for a function. Vlw

  • I think it’s duplicate https://answall.com/q/87638/101 or https://answall.com/q/43948/101.

2 answers

1

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);
}

0

Try putting it like that:

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

In which mat is a multidimensional vector, and you pass the dimension of the matrix

Browser other questions tagged

You are not signed in. Login or sign up in order to post.