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 365409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:32:03+00:00 2026-05-12T13:32:03+00:00

There is this macro offsetof in C/C++ which allows you to get the address

  • 0

There is this macro offsetof in C/C++ which allows you to get the address offset of a member in a POD structure. For an example from the C FAQ:

struct foo {
int a;
int b;
};

struct foo;

/* Set the b member of foo indirectly */
*(int *)((char *)foo + offsetof(b)) = 0xDEADBEEF;

Now this just seems evil to me and I can’t see many legit uses of this macro.

One legit example I have seen is it’s use in the container_of macro in the Linux Kernel for getting the address of an embedded structures parent object:

/* get the address of the cmos device struct in which the cdev
   structure which inode points to is embedded */
struct cmos_dev *cmos_devp = 
     container_of(inode->i_cdev, struct cmos_dev, cdev);

What other legit uses are there for this macro? When should you not use this macro?

EDIT So far this answer to a different SO question is the best one I’ve seen so far.

  • 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-05-12T13:32:04+00:00Added an answer on May 12, 2026 at 1:32 pm

    Well … In C, it’s very useful for any place where you need code to describe a data structure. I’ve used it e.g. to do run-time-generated GUI:s for setting options.

    This worked like this: a command that needs options defines a local structure holding its options, and then describes that structure to the code that generates the GUI, using offsetof to indicate where fields are. Using offsets rather than absolute addresses allows the GUI code to work with any instance of the struct, not just one.

    This is bit hard to sketch quickly in an example (I tried), but since comments indicate an example is in order, I’ll try again.

    Assume we have a self-contained module, called a “command”, that implements some action in the application. This command has a bunch of options that control its general behaviour, which should be exposed to the user through a graphical user interface. For the purposes of this example, assume the application is a file manager, and the command could be e.g. “Copy”.

    The idea is that the copy code lives in one C file, and the GUI code in another, and the GUI code does not need to be hard-coded to “support” the copy command’s options. Instead, we define the options in the copy file, like so:

    struct copy_options
    {
      unsigned int buffer_size;     /* Number of bytes to read/write at a time. */
      unsigned int copy_attributes; /* Attempt to copy attributes. */
      /* more, omitted */
    };
    
    static struct copy_options options; /* Actual instance holding current values. */
    

    Then, the copy command registers its configuration settings with the GUI module:

    void copy_register_options(GUIModule *gui)
    {
      gui_command_begin(gui, "Copy");
      gui_command_add_unsigned_int(gui, "Buffer size", offsetof(struct copy_options, buffer_size));
      gui_command_add_boolean(gui, "Copy attributes", offsetof(struct copy_options, copy_attributes));
      gui_command_end(gui);
    }
    

    Then, let’s say the user asks to set the copy command’s options. We can then first copy the current options, to support cancelling, and ask the GUI module for a dialog holding controls, built at run-time, suitable for editing this command’s options:

    void copy_configure(GUIModule *gui)
    {
      struct copy_options edit = options;
    
      /* Assume this opens a modal dialog, showing proper controls for editing the
       * named command's options, at the address provided. The function returns 1
       * if the user clicked "OK", 0 if the operation was cancelled.
      */
      if(gui_config_dialog(gui, "Copy", &edit))
      {
        /* GUI module changed values in here, make edit results new current. */
        options = edit;
      }
    }
    

    Of course, this code assumes the settings to be pure value-types, so we can copy the struct using simple struct assignment. If we also supported dynamic strings, we’d need a function to do the copying. For configuration data though, any string would probably best be expressed as a statically-sized char array in the struct, which would be fine.

    Note how the fact that the GUI module only knows where each value lives expressed as an offset allows us to provide the dialog function with a temporary on-stack copy. Had we instead set up the GUI module with direct pointers to each field, this would not be possible which would be far less flexible.

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

Sidebar

Related Questions

There's this already populated database which came from another dev. I'm not sure what
I have this macro code, which allows me to define both a C enum
I'm wondering if there's a simple way for a Word macro to determine which
Possible Duplicates: Does the 'offsetof' macro from <stddef.h> invoke undefined behaviour? dereferencing the null
Question: How can I access a member variable in assembly from within a non-POD
Is there a good way to indicate incorrect use of Twig macro from inside
In earlier versions of Visual Studio, there was a predefinied macro _CPPLIB_VER which reported
I've been using the macro from this blog entry for attaching the Visual Studio
For example, never define a macro like this: #define DANGER 60 + 2 This
There is this output of objdump on some object file: $ objdump -h main.o

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.