I have a function that receives 3 pointers of the type float, inside the function what I do is create a pointer that points to the value received by parameters, and I try to add a value in the memory position, but when printing it it throws me that it is 0 (Which is what I initialize it with)
int main(){
float lodgingCost = 0;
float foodCost = 0;
float transportCost = 0;
menuMantenimient(&lodgingCost, &foodCost, &transportCost);
}
int menuMantenimient(float lodgingCost, float foodCost, float transportCost) {
float *buffLodging = &lodgingCost; //Asigno la direccion de memoria ;
float *buffFood = &foodCost;//Asigno la direccion de memoria ;
float *buffTransport = &transportCost;//Asigno la direccion de memoria ;
buffLodging = 100,0;//Agrego un valor a la posicion de memoria
buffFood = 1000,0;//Agrego un valor a la posicion de memoria
buffTransport = 10000,0;//Agrego un valor a la posicion de memoria
printf("%.2f %.2f %.2f",buffLodging ,buffFood,buffLodging); // Imprime 0 en las 3
}
The code should not compile for various reasons.
Float literals divide their decimal part with a period and end with
f
.Without the
f
are literals of typedouble
.Parameters do not have the same memory addresses as your function variables
main
. If the idea is to modify the variables ofmain
or any other function, you would see that no change is made to them. I mean this part of the code:You are passing an array of pointers to the function:
But the function receives values of type float:
To access the value pointed to by a pointer (dereference it) you must use the operator
*
:Finally the code would look like this: