Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8945975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:23:31+00:00 2026-06-15T12:23:31+00:00

The container_of() macro in the Linux kernel is defined as: #define container_of(ptr, type, member)

  • 0

The container_of() macro in the Linux kernel is defined as:

#define container_of(ptr, type, member) ({ \
        const typeof( ((type*)0)->member) * __mptr =(ptr);\
        (type*)( (char*)__mptr - offsetof(type,member) );})

Why does this use ((type*)0)->member, not (type*)->member?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:23:32+00:00Added an answer on June 15, 2026 at 12:23 pm

    Why this is ((type*)0)->member, not (type*)->member

    Simply because (type*)->member would be invalid syntax, thus typeof would be impossible. So it uses a NULL pointer, which it doesn’t dereference anyway – it’s used just so typeof can refer to the member.


    How this works:

    • The typeof trick is used to declare a pointer of the type of the member. This pointer gets is initialized with the pointer passed by the caller

    • The offset of that member in the struct is subtracted from the address of the pointer: this yields the address of the containing object


    Subtler issue: why not get rid of typeof and just do ptr - offsetof. We’re casting it to char * anyway, right ? In that case you could pass anything as ptr and the compiler won’t say a thing. So the whole typeof things is there for (rudimentary) type checking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In linux, container_of macro is enclosed in seemingly extra parentheses: #define container_of(ptr, type, member)
How do you read the second line of this macro? What does (type *)0
Consider the following macro definitions and invocation: #define x x[0] #define y(arg) arg y(x)
There is this macro offsetof in C/C++ which allows you to get the address
Is it possible to implement the container_of macro in pure C90? I'm not sure
While looking at Linux kernel's implementation of doubly linked circular lists, I've found following
we have several tens of macro enabled excel files, each of those contains few
Given the following code (it's a macro that generates code for a list data
I'm working on a Word 2007 template with a macro that will apply character
I want to write a macro to write a string, using the compile-time optimisation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.