M
Michael Goerz
Hi,
What is the error in the code below? The output is:
DEBUG: matrix.b[0] is 2.000000
DEBUG: matrix->b[0] is 0.000000
In other words, the struct is empty after it is passed to the
solve_tridiag function. How can I fix this? I didn't see this in the
FAQ, although I have the feeling that I'm missing something basic here.
Many Thanks,
Michael Goerz
#include <stdio.h>
struct Tridiag{
// represents a tridiagonal matrix like this:
// b1 c1 0 0 0 ...
// a2 b2 c2 0 0 ...
// 0 a3 b3 c3 0 ...
// ... ...
double *a;
double *b;
double *c;
};
struct Tridiag create_matrix(){
struct Tridiag matrix;
double b[5] = { 2.0, 2.0, 2.0, 2.0, 2.0};
double a[5] = { 0.0, -1.0, -1.0, -1.0, -1.0};
double c[5] = {-1.0, -1.0, -1.0, -1.0, 0.0};
matrix.b = b;
matrix.a = a;
matrix.c = c;
return matrix;
}
void solve_tridiag( struct Tridiag *matrix, double *r){
printf("DEBUG: matrix->b[0] is %f\n", matrix->b[0]);
// ...
}
int main(){
double r[] = {0.0 , 1.0, 2.0, 3.0, 4.0};
struct Tridiag matrix = create_matrix();
printf("DEBUG: matrix.b[0] is %f\n", matrix.b[0]);
solve_tridiag(&matrix, r);
return 0;
}
What is the error in the code below? The output is:
DEBUG: matrix.b[0] is 2.000000
DEBUG: matrix->b[0] is 0.000000
In other words, the struct is empty after it is passed to the
solve_tridiag function. How can I fix this? I didn't see this in the
FAQ, although I have the feeling that I'm missing something basic here.
Many Thanks,
Michael Goerz
#include <stdio.h>
struct Tridiag{
// represents a tridiagonal matrix like this:
// b1 c1 0 0 0 ...
// a2 b2 c2 0 0 ...
// 0 a3 b3 c3 0 ...
// ... ...
double *a;
double *b;
double *c;
};
struct Tridiag create_matrix(){
struct Tridiag matrix;
double b[5] = { 2.0, 2.0, 2.0, 2.0, 2.0};
double a[5] = { 0.0, -1.0, -1.0, -1.0, -1.0};
double c[5] = {-1.0, -1.0, -1.0, -1.0, 0.0};
matrix.b = b;
matrix.a = a;
matrix.c = c;
return matrix;
}
void solve_tridiag( struct Tridiag *matrix, double *r){
printf("DEBUG: matrix->b[0] is %f\n", matrix->b[0]);
// ...
}
int main(){
double r[] = {0.0 , 1.0, 2.0, 3.0, 4.0};
struct Tridiag matrix = create_matrix();
printf("DEBUG: matrix.b[0] is %f\n", matrix.b[0]);
solve_tridiag(&matrix, r);
return 0;
}