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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:15:19+00:00 2026-06-11T18:15:19+00:00

In our code we have a big struct of mixed types, and want to

  • 0

In our code we have a big struct of mixed types, and want to store a duplicate (const) struct of default values for these.

When the user want to default a setting, it would be nice to be able to do this by just taking the address offset of the item within the struct, and then assign the value with the same offset in the “defaults” struct, a bit like this:

void *setting = &settings->thing; // Points to a setting
int offset = setting - &settings;
void *default = &defaults_struct + offset; // Points to the default value
*setting = *default; // Set setting to default value

The idea being that if settings->thing points to an int8, the int8 value is copied from defaults, but if settings->other_thing is an int32, the full 32-bits are copied over.

The question is, does this work with void pointers as I’ve described above? If not, is there a way of doing this? Am I missing a better way of achieving this?

Edit to clarify: We want to set a single value within the struct to the corresponding value within the “defaults” struct.

  • 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-11T18:15:20+00:00Added an answer on June 11, 2026 at 6:15 pm

    No, this doesn’t work. Pointers of type void * don’t have a size associated with them, i.e. it’s unknown what data type they point at. It follows that you can’t do the assignment like that, either.

    You should probably just copy from the defaults directly, as suggested in a comment.

    Also note that default is a reserved word in C, so you shouldn’t use it for a variable.

    If the defaults are globally visible, you might define a macro to hide this:

    #define INITIALIZE(s, f) s. f = default_struct. f
    

    The user (which I assume is a developer) can now do:

    struct settings s;
    
    INITIALIZE(s, thing);
    

    That said, I would (as a developer) prefer something like:

    struct settings s = get_default_settings();
    

    This is more clear, and less magic. Sure, it probably takes more time to copy all the fields, but settings are generally not performance-critical.

    Note that the above function might just be:

    struct settings get_default_settings(void)
    {
      static const struct settings the_defaults = {/* the default values go here*/};
    
      return the_defaults;
    }
    

    Which neatly encapsulates the default values, and removes the global.

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

Sidebar

Related Questions

In our code we used to have something like this: *(controller->bigstruct) = ( struct
In our code we have quite a few cases of this pattern: class outerClass
In our code I have the following, for now please ignore the //* bits;
We have recently started using FxCop on our code base and I am in
I have introduced boost to our code base, on my machine I created a
We have this anonymous function in our code, which is part of the jQuery's
How much of an impact will migrating to Flex 4 have on our code
Ok, We have a lot of where clauses in our code. We have just
Our website code is written in PHP. We have very little testing in regards
Background: We have a TFS server setup where me manage our source code and

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.