passing arguments from incompatible pointer type ,warning in the function call Transpose in the main routine.

#include <stdio.h>
#include <stdlib.h>

int size_b_row1;
int size_b_col1;


int main(void) {

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);

puts("Hello World"); /* prints Hello World */
return EXIT_SUCCESS;
}


void transpose(double** matrix_t, double** matrix)
{

int i,j;

/* allocate memory for matrix */

matrix_t=(double **) malloc(sizeof(double *)*size_b_row1);
for( i=0;i<size_b_row1;i++)
{
matrix_t[i]=(double *) malloc(sizeof(double*)*size_b_col1);
}

matrix=(double **) malloc(sizeof(double *)*size_b_row1);
for( i=0;i<size_b_row1;i++)
{
matrix[i]=(double *) malloc(sizeof(double*)*size_b_col1);
}

//output matrix

for(i=0;i<size_b_col1;i++)
{
for(j=0;j<size_b_row1;j++)
{
matrix_t[j][i]= matrix[i][j];
}

}

//free memory
for(i=0;i<size_b_row1;i++)
{
free(matrix[i]);
}
free(matrix);


for(i=0;i<size_b_row1;i++)
{
free(matrix_t[i]);
}
free(matrix_t);
} // end of main body
It is array of arrays, not array of pointers to another arrays.
Arrays are not a pointers. Stack-based multidimensionals arrays are evil. Now you know why.

EDIT: if you have C++11 compiler, following will work:
1
2
3
4
double** C = new double*[4]{new double[4]{1,3,5,2}, new double[4]{7,6,2,2},
                            new double[4]{1,2,7,3}, new double[4]{2,3,5,3}};
double** TC = new double*[4]{new double[4], new double[4],
                             new double[4], new double[4]};
But you will have memory leak in your transpose function.

EDIT2:: WTF transpose function does? It doesn't even using passed parameters. It uses C-style memory manipulation (discouraged in C++) and on top of that has undefined behavior due to uninitializated variables.
Last edited on
Thank you so much for the response, it is actually only some part of my program, so the variables are not initialized here, that is why i declared them dynamically. and i will try to modify the changes you have told.thank you very much again!!
Topic archived. No new replies allowed.