2.6.32
2.6.32

Reputation: 35

container_of macro when we have a pointer inside a struct

If I have:

struct my_container {
    int x;
    struct some_struct *ss;
}

If I have the pointer ss through which I can access the members inside some_struct, I should be able to access my_container by doing the following right ? This is what I am doing:

struct my_container *my_c;
my_c = container_of(&ss, struct my_container, ss)

But this is not working for sure and I am not able to comprehend why. Can someone help me with this? Is there something that I am missing ?

Upvotes: 3

Views: 5633

Answers (1)

Drew McGowen
Drew McGowen

Reputation: 11706

If you only have a pointer to some_struct (i.e. if you just have struct some_struct *ss;), you cannot use the container_of macro in this way, as &ss will just evaluate to the address of some variable, not the address of my_container. To use it properly, you'll need a pointer to a pointer to some_struct (i.e. struct some_struct **pss).

Upvotes: 4

Related Questions