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:
creates a copy.
will do it. Or, if you must have an auxiliary variable for whatever reasons:
This way, both variables will point to the same object.