0

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 ?

mikecolux95
  • 83
  • 1
  • 10
  • First find a [good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and learn how to call functions. Then continue reading the books and learn about *scopes* and what happens with local variables once the function they were defined in returns. – Some programmer dude Apr 29 '16 at 14:16
  • Note: Code will eventually need with a companion `Mat_free(Mat *);` – chux - Reinstate Monica Apr 29 '16 at 16:23
  • There is no matrix (aka 2D array) in your code and nothing which can be used as one. A pointer is not an array and vice versa. – too honest for this site May 12 '16 at 15:16

2 Answers2

2

int rows and int cols are uninitialized going into Mat_alloc. You need to give those numerical values!

int main(int argc, char **argv) 
{
      int rows = 10;
      int cols = 10;
      Mat *m1 = Mat_alloc(rows, cols);

      //do something
      //call your Mat_free(m1) function

     return 0;
}

Make sure you return the pointer to a Mat structure in this function too:

Mat* Mat_alloc(int rows, int cols)
{
    Mat *m1 = malloc(sizeof(Mat));
    m1->rows = rows;
    m1->cols = cols;
    float** matrice= (float**)malloc((m1->rows)*sizeof(float*));
    for(int i = 0; i < m1->rows; i++)
    {
        matrice[i] = (float*)malloc((m1->cols)*sizeof(float));
    }
    m1->row_ptrs = matrice;
    return m1;
}

Also, make sure you create a Mat_free function to free up the memory allocated in Mat_alloc.

1

chrisd1100 gave a good answer, but only to be a little bit pedantic this is mine:

#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(void)
{
    int i;

    int rows = 10;
    int cols = 10;

    Mat *m1 = Mat_alloc(rows, cols);

    for (i=0; i<cols; i++)
    {
        free(m1->row_ptrs[i]);
    }
    free(m1->row_ptrs);
    free(m1);

    return 0;
}

Mat* Mat_alloc(int rows, int cols)
{
    Mat *m1 = malloc(sizeof(Mat));
    m1->rows = rows;
    m1->cols = cols;
    m1->row_ptrs = malloc((m1->rows)*sizeof(float*));
    for(int i = 0; i < m1->rows; i++)
    {
        m1->row_ptrs[i] = malloc((m1->cols)*sizeof(float));
    }

    return m1;
}
LPs
  • 16,045
  • 8
  • 30
  • 61