I have a two-dimensional array dynamically allocated in my C code, in my main function. I need to pass this 2D array to a function. Since the columns and rows of the array are runtime variables, I know that one way to pass this is:
-Remove row and column variables and pointer to this array element [0] [0]
myfunc(&arr[0][0],rows,cols)
then in the called function, refer to it as a "flattened" 1D array, for example:
ptr[i*cols+j]
But I don’t want to do this because it will mean a lot of changes in the code, because previously the 2D array passed to this function was statically distributed with its sizes known at compile time.
So, how can I pass a 2D array to a function and still use it as a 2D array with two indices, such as:
arr[i][j].
.