How to return a matrix in C?

1

My problem is a matrix declared in a function as I do to have the values of the matrix in a second function that prints it, something like this:

#include<stdio.h>
#include<conio.h>
#include<time.h>
void main(){
int m,n;
printf("ingrese 2 numeros:");
scanf("%i %i",&m,&n);
declarar(m,n);\RETORNAR LA MATRIZ
imprimir(matrix,m,n);\USAR LA MATRIZ ANTERIOR
getch();
}
void declarar(int a,int b){
srand(time(NULL));
int i,j,matrix[a][b];
for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        matrix[i][j]=1+rand()%7;
    }
}
}
void imprimir(int c[100][100],int a,int b){
int i,j;
for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        printf("%i\t",c[i][j]);
    }
    printf("\n");
}
} 

How do I return the matrix to use it in the next function?

    
asked by Andrés HM 04.07.2018 в 02:06
source

2 answers

1

The matrix you have declared is a VLA and is something not supported by the standard since C99, that is, since 1999.

int matrix[a][b]; // VLA

If you compile it is because your compiler offers you that functionality, but this functionality does not have to be portable to other compilers or, of course, to more modern versions of your compiler. The issue of the VLA has been discussed on other occasions: link 1 , link2

The fact is that the use of VLA has some restrictions, as you have been able to verify. The solution is to use dynamic memory:

int** declarar(int a,int b){
  srand(time(NULL));
  int i,j,**matrix;

  matriz = (int**)malloc(a*sizeof(int*));
  for(i=0;i<a;i++){
    matriz[i] = (int*)malloc(b*sizeof(int));
    for(j=0;j<b;j++){
        matrix[i][j]=1+rand()%7;
    }
  }
  return matriz;
}

Of course, when the matrix is not necessary, you have to remember to release the memory:

for( int i=0;i<b;i++ )
   free(matriz[i];
free(matriz);
    
answered by 04.07.2018 в 08:48
0

Your problem is that the matrix is declared within the scope (scope) of the declare function, so it creates the array in the stack ) . at the moment of returning the stack they decrease eliminating any refence of the variables declared within this function because it is understood that it will no longer be used.

When entering a function or within a loop or just inside a bracket {} the stack grows and this keeps all the declared variables inside these brackets, when leaving the stack decreases destroying the reference of each Declared variable inside the bracket

//Global Scope
int G;

int main()
{
    //Primer Scope de la funcion main
    int A;
    printf("%p\n", &A);
    printf("%p\n", &G);

    {
        //Segundo Scope de la funcion main
        int B;
        printf("%p\n", &B);
        printf("%p\n", &A);
        printf("%p\n", &G);
    }
}

int func()
{
    //Primer Scope de la funcion func
    int A;
    printf("%p\n", &A);
    printf("%p\n", &G);
}

In the following example I show that variable B does not exist outside the second scope, so it can not be used in the first scope, but variable A was declared in the first scope and since the second scope is inside the The first scope can use the variable A. Now neither A nor B that were declared inside main can be used outside of this because they are local variables to the main function. The variable A inside the function func is different from the variable A inside the main function because they are in different scope. And the G variable like this in the Main or Global Scope can be used within each function in that file.

You can solve your problem by declaring global int ** matrix or within the scope of main.

//1. Glogal
int ** matrix;
void main()
{
    int m,n;
    printf("ingrese 2 numeros:");
    scanf("%i %i",&m,&n);
    declarar(m,n);\RETORNAR LA MATRIZ
    imprimir(matrix,m,n);\USAR LA MATRIZ ANTERIOR
    getch();
}

void declarar(int a, int b)
{
    srand(time(NULL));
    int i,j;

    matriz = (int**)malloc(a*sizeof(int*));
    for(i=0;i<a;i++){
        matriz[i] = (int*)malloc(b*sizeof(int));
        for(j=0;j<b;j++){
            matrix[i][j]=1+rand()%7;
        }
    }
}

void imprimir(int ** c,int a,int b)
{
    int i,j;
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            printf("%i\t",c[i][j]);
            free(c[i][j]);
        }
        printf("\n");
        free(c[i]);
    }
} 

//2. Local a Main
void main()
{
    int m,n;
    int ** matrix;
    printf("ingrese 2 numeros:");
    scanf("%i %i",&m,&n);
    declarar(m,n, &matrix);\RETORNAR LA MATRIZ
    imprimir(matrix,m,n);\USAR LA MATRIZ ANTERIOR
    getch();
}

void declarar(int a, int b, int *** matrix)
{
    srand(time(NULL));
    int i,j;

    (*matrix) = (int**)malloc(a*sizeof(int*));
    for (i=0; i<a; i++){
        (*matrix)[i] = (int*)malloc(b*sizeof(int));
        for(j=0; j<b; j++){
            (*matrix)[i][j]=1+rand()%7;
        }
    }
}

void imprimir(int ** c,int a,int b)
{
    int i,j;
    for(i=0; i<a; i++){
        for(j=0; j<b; j++){
            printf("%i\t",c[i][j]);
            free(c[i][j]);
        }
        printf("\n");
        free(c[i]);
    }
} 

the stack (Stack) stores the calls and the parameters to each function while the code is executed, it grows and is destroyed dynamically and automatically.

the heap reserves memory for the dynamic variables that you need in the execution of the program, it grows and is destroyed dynamically but not automatically. when using the malloc function, memory of a specific size is reserved and by using free this memory is freed.

    
answered by 06.07.2018 в 17:02