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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:11:05+00:00 2026-06-03T16:11:05+00:00

I have the following two structures. I need to copy d, e, f from

  • 0

I have the following two structures. I need to copy d, e, f from source to destination using memcpy and offsetof. How can I do this?

struct source
{
    int a;
    int b;
    int c;
    int d;
    int e;
    int f;
};

struct destination
{
    int d;
    int e;
    int f;
};
  • 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-03T16:11:07+00:00Added an answer on June 3, 2026 at 4:11 pm

    Generally, You cannot reliably do it using memcpy, because the compiler is allowed to pad the two structures differently. So the safest way of performing a partial copy would be to assign the three fields individually.

    However, since the compiler inserts padding only between members with different alignment requirements, in your specific case you can use memcpy like this:

    struct source s {1,2,3,4,5,6};
    struct destination d = {100, 200, 300};
    memcpy(&d, ((char*)(&s))+offsetof(struct source,d), offsetof(struct source,f)-offsetof(struct source,d)+sizeof(int));
    

    The offset of destination‘s d is guaranteed to be zero because it is the initial member of the structure. Since members d, e, and f have identical alignment requirements, the padding, if any, will go after them in the struct destination, and before or after them in the struct source.

    Cast to char* is required because the offset is expressed in bytes.

    The expression

    offsetof(struct source,f)-offsetof(struct source,d)+sizeof(int)
    

    is the length of the run between d and f, inclusive. Note that using sizeof(struct destination) is not safe, because it may have padding at the end which is not present in struct source, causing a read past the allocated memory.

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

Sidebar

Related Questions

I hope I can explain this clearly: I have two tables with the following
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
Need help with following situation: Users can generate their own data structures which are
I have two dictionaries of the following structure: a) dict1 = {'a':[ [1,2], [3,4]
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two Excel files contains the following structure, EmployeeAllDtl.xlsx id email name age
I have following two arrays. I want the difference between these two arrays. That
How to wrap one div around another? I have following two div ids: #course

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.