Hi,
I am getting Segmentation fault error for code below. Please tell me how to remove it.
Code:
#include<stdio.h>
#include<stdlib.h>
struct MATRIX {
int rows;
int cols;
double **t;
};
struct MATRIX new_matrix(int rows, int cols)
{
struct MATRIX t;
int i;
t.rows=rows;
t.cols=cols;
t.t=(double **)malloc(t.rows * sizeof(double *));
for(i = 0; i < t.rows; i++)
t.t[i] = (double *) malloc(cols * sizeof(double));
return t;
}
struct MATRIX *transpose(struct MATRIX *a)
{
struct MATRIX *b;
int i, j;
*b = new_matrix(a->cols, a->rows);
for(i=0; i<b->rows; i++)
for(j=0; j<b->cols; j++)
*(*(b->t+i)+j)=*(*(a->t+j)+i);
return b;
}
int main(){
struct MATRIX *x;
struct MATRIX *t;
int i, j;
*x = new_matrix(3, 4);
for(i = 0; i < x->rows; i++)
for(j = 0; j < x->cols; j++)
*(*(x->t + i) + j) = 1.2*(i+j);
t = transpose(x);
for(i=0; i < x->rows; i++) {
printf("| ");
for(j=0; j < x->cols; j++)
printf("%.2f ",*(*(x->t+i)+j));
printf("|\n");
}
printf("\n");
for(i=0; i < t->rows; i++) {
printf("| ");
for(j=0; j < t->cols; j++)
printf("%.2f ",*(*(t->t+i)+j));
printf("|\n");
}
printf("\n");
}