return array;
is one problem in you code. Apart from that one more big problem is there.
float** array= malloc(sizeof(float*) * y);
- You are first allocating memory for array to store array of pointers(to float). And then you are allocating memory for each row malloc(sizeof(float) * x);
Now you have allocated memory for 2 rows and 100 columns.Then Call generateData(arr, 100, 2);
. In generateData
if you do like inData[100][2]
this will leads to crash. Because you have allocated only 2 rows and each rows has 100 elements. inData[100][2]
is equivalent to *( *(inData + 100) + 2)
. Here *(inData + 100)
will leads to crash(undefined behaviour). Because you are suppose to access only upto index 1
not 100
.
Mistake is in createMatrix
function, you have wrongly used the x
and y
. Rewrite your function like below.
float** createMatrix(int x, int y)
{
int i;
float** array= (float **)malloc(sizeof(float*) * x);
for(int i=0; i<x; i++)
{
array[i] = (float *)malloc(sizeof(float) * y);
}
return array;
}
And also rewrite your generateData
also as below.
void generateData(float** inData, int x, int y)
{
x--;
y--;
inData[i][j]= 3000.0; //Do you assignment to array here
}