4 ms·
void employee_set_age(struct Employee* employee, int newAge) { // Cast away the const and set it's value, the compiler should optimize this for you (int)&empl
by fpotier 10mo ago
void employee_set_age(struct Employee* employee, int newAge) {
// Cast away the const and set it's value, the compiler should optimize this for you
(int)&employee->age = newAge;
}
I believe that "Casting away the const" is UB [1]
[1]: https://en.cppreference.com/w/c/language/const.html https://en.cppreference.com/w/c/language/const.html
- spacechild1 10mo agoIt's only UB if the pointed to object is actually const (in which case it might live in read-only memory).
- fpotier 10mo ago[dead]