Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

can't access the value of a pointer inside a returned struct [C]

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know how your p_middle(point_t p1, point_t p2) function is implemented but I am sure that your p_start_coords pointer is pointing to wrong address. I wrote it like this just for testing, and it compiled, worked without any problem:

point_t p_middle (point_t p1, point_t p2) {

    point_t p_middle;
    p_middle.p_name = 'M';
    p_middle.p_dim = 100;
    p_middle.p_start_coords = p1.p_start_coords;
    p_middle.p_end_coords = p2.p_end_coords;

    return p_middle;
}

Try to assign valid address to your pointers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...