I am trying to create an abstract data type for matrices. I have a function matrixInit to dynamically allocate memory for the structure and for the array. matrixCopy copies one matrix into another. there are functions to subtract, add and multiply matrices that return a pointer to memory where the Matrix structure is. I have included my .c file and my .h file below. I'm not exactly sure what's not working. I would appreciate it if someone could look at what I have and let me know where I'm going wrong. I believe that my first function is correct, and my destroy functions is also correct. I think the problem is in matrixCopy. I have also commented out sections so that everything would compile. the getelement function and setelement both cause segmentation faults and I can't figure out why.

```
# include <stdio.h>
# include <stdlib.h>
# include "matrix.h"
void matrixInit (Matrix* m , int r, int c)
{
m= (Matrix*)malloc(sizeof(Matrix));
int i, j;
m->cols = c;
m->rows = r;
if (r != 0 & c != 0)
{
m ->element = (int**) malloc(sizeof(int*)*r);
for (i = 0; i < r; i++)
{
m->element[i]= (int*) malloc( sizeof( int)*c);
for(j=0; j<c; j++)
{
m->element[i][j]=0;
}
}
}
else
{
m->element= NULL;
}
}
void matrixCopy (Matrix* m, Matrix* n)
{
int i, j;
matrixDestruct(m);
if( n->element !=NULL)
{
m ->element = (int**) malloc(sizeof(int*)*n->rows);
for (i = 0; i < n->rows; i++)
{
m->element[i]= (int*) malloc( sizeof( int)*n->cols);
for(j=0; j< n->cols; j++)
{
m->element[i][j] = n->element[i][j];
}
}
}
}
int matrixGetElement (Matrix* m, int r, int c)
{
//return m->element[r][c];
}
int matrixGetRows (Matrix* m)
{
return m->rows;
}
int matrixGetCols (Matrix* m)
{
return m->cols;
}
void matrixSetElement (Matrix* m, int r, int c, int v)
{
//m->element[r][c]=v;
}
Matrix* matrixAdd ( Matrix* m, Matrix* n)
{/*
int i, j;
Matrix *k;
k= (Matrix*)malloc(sizeof(Matrix));
k->cols = m->cols;
k->rows = m->rows;
k ->element = (int**) malloc(sizeof(int*)*m->rows);
for (i = 0; i < m->rows; i++)
{
k->element[i]= (int*) malloc( sizeof( int)*m->cols);
for(j=0; j< m->cols; j++)
{
k->element[i][j] = n->element[i][j]+ m->element[i][j];
}
}
return k;*/
}
Matrix* matrixSubtract ( Matrix* m, Matrix* n)
{/*
int i, j;
Matrix *k;
k= (Matrix*)malloc(sizeof(Matrix));
k->cols = m->cols;
k->rows = m->rows;
k ->element = (int**) malloc(sizeof(int*)*m->rows);
for (i = 0; i < m->rows; i++)
{
k->element[i]= (int*) malloc( sizeof( int)*m->cols);
for(j=0; j< m->cols; j++)
{
k->element[i][j] = m->element[i][j] - n->element[i][j];
}
}
return k;*/
}
Matrix* matrixMultiply( Matrix* m, Matrix* n)
{/*
Matrix* k;
int i, j, sum, x=0;
k= (Matrix*)malloc(sizeof(Matrix));
k->cols = m->cols;
k->rows = m->rows;
k ->element = (int**) malloc(sizeof(int*)*m->rows);
for (i = 0; i < m->rows; i++)
{
k->element[i]= (int*) malloc( sizeof( int)*m->cols);
}
if(m->cols==n->rows )
{
for(i=0; i< (m->rows); i++)
{
sum=0;
for(j=0; j< (n->cols); j++)
{
sum+= m->element[i][j] * n->element[j][i];
while(j==n->cols)
{
k->element[i][j]=sum;
}
}
}
return k;
}
else
return NULL;*/
}
void matrixDestruct( Matrix* m)
{
int i;
if( m->element != NULL)
{
for ( i=0; i < m->rows; i++)
{
free(m->element[i]);
}
free(m->element);
}
}
```

here is my .h file

```
typedef struct{
int rows, cols;
int **element;
}Matrix;
void matrixInit (Matrix* , int, int);
void matrixCopy (Matrix*, Matrix* );
int matrixGetElement (Matrix*, int, int);
int matrixGetRows (Matrix*);
int matrixGetCols (Matrix*);
void matrixSetElement (Matrix*, int, int , int);
Matrix* matrixAdd ( Matrix*, Matrix*);
Matrix* matrixSubtract ( Matrix*, Matrix*);
Matrix* matrixMultiply( Matrix*, Matrix*);
void matrixDestruct( Matrix*);
```