结构的成员不会存储变量

时间:2017-11-20 13:49:13

标签: c struct output

我今天的代码有问题, 问题看起来像这样:

void my_function(r_struct *suit) 
{
     sfVector2f position;

     position = get_position(suit->other_vector);
     printf("Output : %f\n", position.x);
}

(get_position返回sfVector2f) 当我这样做时,位置输出为:0。 但是当我这样做的时候:

void my_function(r_struct *suit)
{
     printf("Output: %f\n", get_position(suit->other_vector).x);
}

现在的输出是:50。我不明白我做错了什么

修改

sfVector2f *get_position(sfVector2f *point_to)
{
     sfVector2f new_pos;
     new_pos.x = point_to.x;
     new_pos.y = point_to.y;
     return (new_pos);
}

1 个答案:

答案 0 :(得分:0)

当您退出该变量将被销毁的函数并且其保留的值将丢失时,您无法返回指向局部变量的指针。 如果您返回sfVector2f,它应按预期工作,并保留其值b。

sfVector2f get_position(sfVector2f *point_to)
{
     sfVector2f new_pos;
     new_pos.x = point_to.x;
     new_pos.y = point_to.y;
     return (new_pos);
}