简体   繁体   中英

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 ?

If you only have a pointer to some_struct (ie 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 (ie struct some_struct **pss ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM