Hi I have a function that get a struct, and sum all his values, and return a float, and i want to print that returned float but, for some reason i get weird number, and i don't know whats happening
Code:
typedefstructcompetitor_status{
float cucumber;
float carrot;
float bean;
struct competitor_status * left; /* links to teams with more or same points */
struct competitor_status * right; /* links to teams with less points */
} competitor_node;
typedef competitor_node * comp_node_ptr;
void printNode(comp_node_ptr node){
if(node == NULL){
printf("Can't print information from one NULL node. \n");
return;
}
float total;
total = totalLength(node);
printf("%-20s %-5d %20.2f %20.2f %20.2f %20.2f \n",
node -> name,
node -> id,
node -> cucumber,
node -> carrot,
node -> bean,
total
);
}
float totalLength(comp_node_ptr node){
float result;
result = 0;
result += node -> carrot;
result += node -> bean;
result += node -> cucumber;
printf("%.2f \n", result);
return result;
}
inside the function totalLength when it prints out they have the value correct 108.40, but when is returned and printed they have 1121504512.00 . I'm confued now, because is not the first time i had this problem
This is the output I have
108.40
Margaret Mouse 1 12.50 39.70 56.20 1121504512.00
First green number is the sum of the other green values, this is produced inside the totalLength method, and the red number is when we print that value