You do not need global variables here. So, you could declare, define and initialize the size of your struct array, as well as the dimensions of your matrices in the main method.
Moreover, your methods' names are misleading, I changed them to something that communicates to the author what every function's purpose is. Or, more accurately your methods do more than one tasks. It's good to divide tasks for reusability.
Here is a minimal example to get you started:
#include <stdlib.h>
#include <stdio.h>
struct MyStruct
{
double **matrix;
};
double **allocate_matrix(int rows, int cols)
{
double **matrix = malloc(rows * sizeof(double*));
for (int i = 0; i < rows; i++)
matrix[i] = malloc(cols * sizeof(double));
return matrix;
}
void allocate_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
structs[i].matrix = allocate_matrix(rows, cols);
}
void fill_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
for(int j = 0; j < rows; j++)
for(int z = 0; z < cols; z++)
structs[i].matrix[j][z] = -1.2;
}
void print_matrices(struct MyStruct *structs, int size, int rows, int cols)
{
for (int i = 0; i < size; i++)
for(int j = 0; j < rows; j++)
for(int z = 0; z < cols; z++)
printf("%f\n", structs[i].matrix[j][z]);
}
void free_matrices(struct MyStruct *structs, int size, int rows) {
for (int i = 0; i < size; i++) {
for(int j = 0; j < rows; j++) {
free(structs[i].matrix[j]);
}
free(structs[i].matrix);
}
}
int main()
{
int rows = 3, cols = 4, size = 2;
struct MyStruct *structs = malloc(size * sizeof(struct MyStruct));
structs[0].matrix = NULL;
structs[1].matrix = NULL;
if(structs[0].matrix == NULL)
printf("null\n");
allocate_matrices(structs, size, rows, cols);
if(structs[0].matrix == NULL)
printf("null\n");
fill_matrices(structs, size, rows, cols);
print_matrices(structs, size, rows, cols);
free_matrices(structs, size, rows);
free(structs);
}
Output:
null
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
-1.200000
Inspired from my 2D dynamic array (C).