In C/C++
, when you pass an array to a function, it decays to be a pointer pointing to first element of the array. So, in pixels()
function, you are returning the address of a stack allocated variable. The returning variable's address is no longer valid because on pixels()
return, the stack allocated variable goes out of scope. So, instead you should for a variable whose storage is dynamic ( i.e., using malloc, calloc ).
So, for a two dimensional array, you may use float** arrayVariable;
. Also, if you passing this to a function, you should be wary of how many rows & columns it has.
int rows, columns;
float** pixels()
{
// take input for rows, columns
// allocate memory from free store for the 2D array accordingly
// return the array
}
void drawLine( float** returnedArrayVariable )
{
//drawing the line
}
Since, 2D array is managing resources it self, it should return the resources back to the free store using free.