gpt4 book ai didi

c - 结构体成员不会存储在变量中

转载 作者:行者123 更新时间:2023-11-30 16:38:43 26 4
gpt4 key购买 nike

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

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);
}

最佳答案

你不能返回指向局部变量的指针,当你退出函数时,该变量将被销毁,并且它所保存的值将丢失。如果您返回一个 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);
}

关于c - 结构体成员不会存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com