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.