0

I am trying to create an abstract data type for a Matrix in C. The struct declaration I have for the abstract matrix data type is the following:

typedef struct matrix_st *Matrix;

And I want to create a new instance of these 3 methods where createMatrix creates the matrix ,checkequals checks to see if 2 given matrixs are equal and checkDuplicate checks to see that they are the same :

Matrix Creatematrix( size_t rows, size_t cols ); 
bool checkequals( const Matrix m1, const Matrix m2 );
Matrix checkDuplicate( const Matrix mat );

How would I structure my struct to be able to follow this format? Would just go ahead and set up a 2d array within the struct? Any help would be appreciated. I am mainly just confused on how to set the matrix as a abstract data type and how I would start implementing it.

  • You can create a struct Matrix { int** Matrix; }, then "overload" the equal operator by doing something like void compareMatrix(struct Matrix A, struct Matrix B) – Omid CompSCI Nov 08 '16 at 17:47
  • 2
    **Never ever** `typedef` pointers! That only leads to confusion and is problematic (at best) to write const-correct code. And being a 3-star C programmer is not a compliment. Keep that in mind. – too honest for this site Nov 08 '16 at 17:51
  • I like to use a matrix type that makes views and transposes and memory management very easy; I've outlined it [in this "answer"](http://stackoverflow.com/a/34862940/1475978). With these, you declare matrix variables as `matrix m = MATRIX_INIT;`, and after you no longer need it, run `matrix_free(&m);`. The actual data *in* the matrix is managed separately, and is released when you free the last matrix needing it. It's quite nice; perhaps you'll find the ideas there useful? – Nominal Animal Nov 08 '16 at 19:07

0 Answers0