I've defined the following struct:
typedef struct {
double salary;
} Employee;
I want to change the value of salary. I attempt to pass it by reference, but the value remains unchanged. Below is the code:
void raiseSalary (Employee* e, double newSalary) {
Employee myEmployee = *e;
myEmployee.salary = newSalary;
}
When I call this function, the salary remains unchanged. What am I doing wrong?
You're passing a pointer to the original, but then you create a copy of it:
Employee myEmployee =*e;
creates a copy.
e->salary = newSalary;
will do it. Or, if you must have an auxiliary variable for whatever reasons:
Employee* myEmployee =e;
Myemployee->salary= newSalary;
This way, both variables will point to the same object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With