So i'm trying to allocat an array to hold a matrix of floats. the values passed in are ints (rows and cols) and the function is a pointer. so this is my function definition:
float *matrix(int rows,int cols)
{
int i=0;
float *m=NULL;
m=(float *)malloc(rows*sizeof(int));
for (i=0;i<rows;i++)
{
m[i]=(float*)malloc(cols*sizeof(int));
}
}
i have a feeling that this is wrong. i also get an error when i try to run. where exactly is the problem here? should the int be a float instead?
edit****
float *matrix(int rows,int cols)
{
int i=0;
float **m=NULL;
m=(float *)malloc(rows*sizeof(float));
for (i=0;i<rows;i++)
{
m[i]=(float *)malloc(cols*sizeof(float));
}
}
alright if malloc doesn't run properly and fails i wan't to return NULL. so it should be this code here, right?
if(m[i]==NULL)
{
return NULL;
}