I must write a function that should help me to allocate a matrix using a struct. I Began to study the structs today. So I wrote this code with that struct and relative main to prove the function:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
int rows;
int cols;
float **row_ptrs;
} Mat;
Mat* Mat_alloc(int rows, int cols);
int main(int argc, char **argv)
{
Mat *m1 = Mat_alloc(int rows, int cols);
return 0;
}
Mat* Mat_alloc(int rows, int cols)
{
Mat matrice;
matrice.rows = rows;
matrice.cols = cols;
float** matrice= (float**)malloc((matrice.rows)*sizeof(float*));
for(int i = 0; i < matrice.cols; i++)
{
matrice[i] = (float*)malloc((matrice.cols)*sizeof(float));
}
matrice.row_ptrs = matrice;
return matrice;
}
I know that I make some mistakes.Someone can help me to undestand how I can do it ?