I have huge problem with pointers from few days, i have written the transpose function in two ways, the one just below the main with malloc never works and the one without malloc in quotes works perfect, i have to write a program using malloc, please can any one help me.
Code:
//the .h file is as follows
int size_b_row1=4;
int size_b_col1= 4;
void transpose();
//void transpose(double matrix[size_b_row1][size_b_col1], double matrix_t[size_b_col1][size_b_row1]);
//the .c file is as follows
int main()
{
double C[4][4] = {{1,3,5,2},{7,6,2,2},{1,2,7,3},{2,3,5,3}};
double TC[4][4];
transpose(C, TC);
printf("%f", TC[3][3]);
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
void transpose()
{
int i,j,k;
double **matrix;
double **matrix_t;
matrix=(double **) malloc(sizeof(double *)*size_b_row1);
for( k=0;k<size_b_row1;k++)
{
matrix[k]=(double *) malloc(sizeof(double*)*size_b_col1);
}
matrix_t=(double **) malloc(sizeof(double *)*size_b_row1);
for( k=0;k<size_b_row1;k++)
{
matrix_t[k]=(double *) malloc(sizeof(double*)*size_b_col1);
}
for(i=0;i<size_b_row1;i++)
{
for(j=0; j<size_b_col1;j++)
matrix_t[j][i] = matrix[i][j];
}
/*
void transpose(double matrix[size_b_row1][size_b_col1], double matrix_t[size_b_col1][size_b_row1])
{
int i,j;
for(i=0;i<size_b_row1;i++)
{
for(j=0; j<size_b_col1;j++)
matrix_t[j][i] = matrix[i][j];
}
}
*/