Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello everyone,
I have a problem when assigning value to an array of structures
for example let's declare the following structure;
C++
typedef struct
{
   float member;
}motor;

now the next step is to declare an array of of type motor
C++
motor joint[4];

now if i assign member like this there is no problem;
C++
joint[1].member = 10;
printf("%f", joint[1].member);

but if i assign member through a function, the value is = 0.0
for example;
C++
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
C#
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
Posted
Comments
Zoltán Zörgő 12-Aug-13 15:19pm    
Are you sure, the function is returning the value you want?
Have you tried to debug and see how variables change?
z3ngew 12-Aug-13 15:20pm    
well, when i prinf the value, it return 0.0
Zoltán Zörgő 12-Aug-13 15:21pm    
Print temp_value before returning it!
If you return 0.0, why do you expect to get another value in the variable you are assigning the returned value?
z3ngew 12-Aug-13 15:26pm    
well look i missed something but still there is a problem
inside the function body there is the following;
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]);

Why is that ?
Zoltán Zörgő 12-Aug-13 15:30pm    
Sorry, I can't figure it out without complete code. I assume you made some other mistake somewhere. Be aware that arrays are passed as pointer, thus you work on the original array, not a copy of it!

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.
 
Share this answer
 
Quote:
float calculate_torque(motor motor_array[], int index, some input)
{
float temp_value;
//body of the function
return temp_value;
}


You are NOT assigning motor_array[index].member inside the function, so no surprise if it is unchanged.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900