I'm pretty new to the C language, and I'm struggling with pointers (I used to program in Python). So, I have a two file project, the main one (.c) and another header (.h). I declared in the header one a custom type point_t and some function, such as p_middle, which calculated the coordinate of the middle point starting from two points. I tested out the function in the header file and there it works, but when I try to use it in the source file it doesn't. The structure point_t is declared as follows:
typedef struct point {
char p_name; // stores the point name as a single character
short p_dim; // stores the point dimension (it can be omitted and calculated using p_dimension)
double *p_start_coords; // pointer to the first coordinate of coordinate list
double *p_end_coords; // pointer to the last coordinate of coordinate list
} point_t;
and the function p_middle has a declaration that looks like:
point_t p_middle (point_t p1, point_t p2) {
point_t p_middle;
// p_middle initialization
// some code here
return p_middle
}
so in the source file I tried creating two points as:
point_t p1;
point_t p2;
double coord_set1[4] = {0, 2, 3, 4};
double coord_set2[4] = {3, 1, 6, 4};
p1.p_start_coords = &coord_set1[0];
p1.p_end_coords = &coord_set1[3];
p1.p_name = 'A';
p2.p_start_coords = &coord_set2[0];
p2.p_end_coords = &coord_set2[3];
p2.p_name = 'B';
I then tried to do in the source file:
p_m = p_middle(p1, p2);
printf("middle point of p1p2: (%f, ", *p_m.p_start_coords);
++p_m.p_start_coords;
printf("%f, ", *p_m.p_start_coords);
++p_m.p_start_coords;
printf("%f, ", *p_m.p_start_coords);
++p_m.p_start_coords;
printf("%f)
", *p_m.p_start_coords);
But, when I try to run the program it doesn't work, it prints rundom - I think - numbers. Any idea for solutions?
P.S. Excuse my English, I'm still practising it.