Trying to run this piece of code:
void print_matrix(matrix* arg)
{
int i, j;
for(i = 0; i < arg->rows; i++) {
for(j = 0; j < arg->columns; j++) { // gdb shows, that arg->columns value
printf("\n %f", arg->data[i][j]); // has been changed in this line (was
} // 3, is 0)
printf("\n");
}
}
matrix is a structure:
typedef struct matrix_t
{
int rows;
int columns;
double** data;
} matrix;
arg is properly allocated 3x3 matrix, rows = 3, columns = 3
Function does print only \n's .
Compiler is gcc 4.5. Any ideas?
EDIT:
int main()
{
matrix arg;
arg.rows = 3;
arg.columns = 3;
arg.data = (double**)malloc(sizeof(double*) * arg.rows);
int i;
for(i = 0; i < arg.rows; i++) {
arg.data[i] = (double*)malloc(sizeof(double) * arg.columns);
}
arg.data[0][0] = 1;
arg.data[0][1] = 2;
//......
print_matrix(&arg);
for(i = 0; i < arg.rows; i++) {
free(arg.data[i]);
}
free(arg.data);
return EXIT_SUCCESS;
}
data? Why isdatadeclared to be a pointer to a pointer?