分配结构数组 [英] assign array of structures
问题描述
大家好,
我在为结构数组赋值时遇到问题
例如让我们声明以下结构;
typedef struct
{
float 成员;
} motor;
现在下一步是声明一个类型 motor的数组
motor joint [ 4 ];
现在如果我指定会员这样没有问题;
<前lang =c ++>联合[ 1 ]。member = < span class =code-digit> 10 ;
printf( %f,联合[ 1 ]。member);
但如果我通过函数指定成员,则值为= 0.0 >
例如;
float calculate_torque(motor motor_array [], int index,some input)
{
float temp_value;
// 函数正文
返回 temp_value;
}
现在,如果我像这样分配
joint [ 1 ]。member = calculate_torque(joint, 1 , // 其他输入);
printf( %f,联合[ 1 ]。member);
值仍然= 0.0 ;
我不是什么问题,或者我错过了什么,
提前谢谢,
z3ngew
很明显,在您的代码中,您没有显示,当您计算扭矩时,您不会将其保存到temp_value或不要将计算出的答案分配给temp_value,因此temp_value的原始值仍为= 0;
temp_value = another_function(joint [index]);
但看起来索引值没有响应d如果我用
替换它
temp_value = another_function(关节[1]);
上面的代码行应该按以下方式阅读:
temp_value = another_function(关联[index] .member);
如果是值您想要计算然后返回的'成员'。
Quote:float calculate_torque( motor motor_array [],int index,some input)
{
float temp_value;
//函数体//
返回temp_value;
}
你没有指定motor_array [index] .member
在函数内部,如果它没有改变就不足为奇了。
Hello everyone,
I have a problem when assigning value to an array of structures
for example let's declare the following structure;
typedef struct
{
float member;
}motor;
now the next step is to declare an array of of type motor
motor joint[4];
now if i assign member like this there is no problem;
joint[1].member = 10;
printf("%f", joint[1].member);
but if i assign member through a function, the value is = 0.0
for example;
float calculate_torque(motor motor_array[], int index, some input)
{
float temp_value;
//body of the function
return temp_value;
}
Now, if i assign it like this
joint[1].member = calculate_torque(joint, 1, //other inputs);
printf("%f", joint[1].member);
the value still = 0.0;
I don't what is the problem, or what i am missing,
Thanks in advance,
z3ngew
It is pretty obvious that in your code, that you are not showing, that when you calc the torque, you are not saving it to temp_value or don't assign the calculated answer to the temp_value, so the original value of temp_value is still = 0;
temp_value = another_function(joint[index]);
but it looks like the index value is not responding and if i replace it with
temp_value = another_function(joint[1]);
THE ABOVE LINE OF CODE SHOULD READ AS THE FOLLOWING:
temp_value = another_function(joint[index].member);
If it is the value of 'member' that you are looking to calculate and then return.
Quote:float calculate_torque(motor motor_array[], int index, some input)
{
float temp_value;
//body of the function
return temp_value;
}
You are NOT assigningmotor_array[index].member
inside the function, so no surprise if it is unchanged.
这篇关于分配结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!