Updated: 2025/Nov/16

Please read Privacy Policy. It's for your privacy.


CONTAINER_OF(3)            Library Functions Manual            CONTAINER_OF(3)

NAME
     container_of - cast a pointer to member of a structure to a pointer of
     its container structure.

SYNOPSIS
     #include <sys/container_of.h>

     type *
     container_of(pointer, type, member);

DESCRIPTION
     Given a pointer that points to a member of the container structure type
     the container_of() macro returns a pointer that points to the enclosing
     container structure.

     A compiler error will result if member is not aligned to a byte boundary
     (i.e. it is a bit-field).

EXAMPLES
     #include <assert.h>
     #include <sys/container_of.h>
     struct container {
             double  other_member;
             int     member;
     };

     struct container one;

     void test(void) {
             int *ptr = &one.member;
             struct container *onep = container_of(ptr, struct container, member);
             assert(onep == &one);
     }

SEE ALSO
     __alignof__(3), offsetof(3), stddef(3), typeof(3)

HISTORY
     The container_of() macro appeared first in the Linux kernel.

NetBSD 11.99                    October 8, 2011                   NetBSD 11.99