I took a hiatus from C and am just getting back into it again.
If I want to create a 2D array of doubles, I can do it two ways:
double** m_array = (double**) malloc(2*sizeof(double*));
double* m_array = (double*) malloc(2*sizeof(double));
OR
double array[2][2];
But, when I wish to pass the malloc’d array versus passing the other, there seems to be two conventions:
//allowed for passing in malloc'd array, but not for other array
func_m(m_array) //allowed
func_m(array) //disallowed
func_m(double** m_array)
//allowed for passing in either array; required for passing in non-malloc'd array
func(m_array) //allowed
func(array) //allowed
func(double array[][2])
In the first, I don’t need any information beyond that it is a pointer to an array of pointers. But it can only be a malloc’d array.
In the second, I need to pass the length of each array that the array of double* points to. This seems silly.
Am I missing something? Thanks in advance.
The first one doesn’t create a 2-D array at all. It creates an array of pointers, which apparently point nowhere. If you did initialize each pointer to be an array, that would still be an array of arrays, not a 2-D array.
Why don’t you just create a 2-D array?
or
and then you can use either one with this function: