I'm aware of what container_of
macro does, but I'm far from understanding it.
I know one can get a struct reference as long as having its member's name, member's type, and member's reference.
For example, consider the code below
struct car //car is one of many kind of vehicles
{
struct vehicle base; //vehicle is anything that moves you.
/*other attributes that car has*/
};
int do_something(struct vehicle* a_vehicle)
{
struct car* a_car = container_of(a_vehicle, struct car, base);
/*do some operations*/
}
The function do_something()
can be treated as a method operating on vehicle object. However, inside the function what really happen is opposite to its name. In other words, the real object being operated on is car, not vehicle.
Why cannot just pass struct car
?
What would do_something()
be like if container_of
was not allowed to use?