To initialize memory for an 2D array within in another function with the intent to use the very same memory outside this function you must use malloc to ask for the required memory from the OS.
Malloc returns an void pointer to the start adress of the block of memory you requested or NULL if the request could not be served from the OS, which nowadays with home PC's is very unlikely to happen because it means you have run out of memory. malloc doc
If you want to create array of pointer of type Int you have to do the following
see
int ** readMatrix(int rows,int cols){
int** var = (int**) malloc(sizeof(int)*rows); //Here you allocate the memory for the pointer which will later point the the columns
for (int i=0; i<r; i++)
arr[i] = (int *)malloc(cols * sizeof(int)); //Here you allocate the columns
return var;
}
another way would be to map an 2D array onto an 1D array. What do I mean with mapping? Let me show it with an example:
If you want to map all Elements from an 2D array onto 1D array the 1D array must have the same number of Elements as the 2D array otherwise you would miss elements.
Then you need some kind of formula to calc the index from 2D array to an 1D array. The formula is: y*width+x
Width represent the maximum amount of elements one row of the 2D array can contain. X represent your position on the current row and Y represent the row you are currently at.
in C it would look like this:
int* var = malloc(sizeof(int)*rows*cols);
for(int i = 0; i < row; i++)
for(int j = 0; j < cols; j++)
var[i*cols+j] = 0;