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

  • Home
  • SEARCH
  • 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 8938873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:43:42+00:00 2026-06-15T10:43:42+00:00

My teacher is having us use the following preprocessor macro in implementing a linked

  • 0

My teacher is having us use the following preprocessor macro in implementing a linked queue in C. The idea is you make your queue generic by having it hold no data, then you have a wrapper struct elsewhere which holds the node in the queue, and the piece of data belonging to it.

The macro below takes in a node from the queue (i.e. node), the type of the wrapper struct (i.e. struct Wrapper), and name of the queue node element of the Wrapper (i.e. qnode (Wrapper has an element called qnode)).

Then the macro returns the struct Wrapper that the node passed in is contained in.

So a call would look like this:

queue_entry(node, struct Wrapper, qnode)

This is EXTREMELY COOL in my eyes, and it works fine, (seeing as how my teacher wrote it, it’d better!).
But I was hoping someone could explain to me how it actually works? Because I’m at a loss for what is actually going on behind the scenes.

The Macro:

#define queue_entry(NODE, STRUCT, MEMBER)               \
    ((STRUCT *)((uint8_t*)(NODE) - offsetof(STRUCT, 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-15T10:43:43+00:00Added an answer on June 15, 2026 at 10:43 am

    The member elements in C struct have fixed difference in their address. This difference gets defined at the compile time itself.

    You can get this difference in address by using &(struct_object.member) - &struct_object. This is what is returned by offsetof.

    e.g. consider below struct:

    struct abcd{
    int a; // 4 bytes
    int b; // another 4 bytes
    char c;// 1 byte
    }
    

    Then offsetof(struct abcd,c) would return 8. offsetof(struct abcd,a) would be 0 & so on. The ‘padding‘ or ‘alignment’ within the struct also plays the role, in deciding the offset. However, this is decided at the compile time, not run time.

    Hence, if you have address of a member (char c in our example), but not the structure, you can obtain the address of the parent structure, by subtracting the member address offset, from member address.

    In your example, address of member is contained in node. Hence if you subtract member ofset, you will get address of container struct.

    In linux kernel source code, same macro is available under the name, container_of (I forgot the capitalization & underscores if any).

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

Sidebar

Related Questions

My teacher and I are having a debate about whether it is possible to
I'm having trouble with an assignment a teacher made. I'm a python newbie so
Having some difficulty understanding the best way to implement subclasses with a generic repository
I am having a issue in identifying the relationship I have the following scenario
We are having a lot of trouble interpreting our teacher. We asked for clarification
I am having some trouble with php and fpdf. I have to make a
I'm having trouble understanding a concept my teacher taught me and getting pretty inconsistent
im having troubles with a web app that a teacher ask me to modify,
My teacher gave out a practice exam on java recently and I'm curious to
My teacher asked me to do a program using execlp that will do the

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.