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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:27:13+00:00 2026-05-20T18:27:13+00:00

I have two objects both of the same type, for instance: typedef struct s_Object

  • 0

I have two objects both of the same type, for instance:

typedef struct s_Object 
{
  signed int a[3]; 
  double float b;
  unsigned short int c[30];
  struct s_Object *next, 
                  *prev;
} t_Object;
t_Object A, B;

I need some generic function like:

swap_vars(&A, &B);

which will not swap pointers but all other data the objects can contain. Is it possible in C?

Assuming pointers are placed in the highest addresses, my first idea was:

void swap_vars(t_Object *pA, t_Object *pB){
   t_Object C;
   memcpy(&C, pA, sizeof(t_Object)-sizeof(t_Object *)*2;
   memcpy(pA, pB, sizeof(t_Object)-sizeof(t_Object *)*2;
   memcpy(pB, &C, sizeof(t_Object)-sizeof(t_Object *)*2;
}

but I don’t know how portable is it (or maybe wrong at all)?

I am looking for a most portable solution.

  • 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-20T18:27:13+00:00Added an answer on May 20, 2026 at 6:27 pm

    Use offsetof:

    memcpy(&C, pA, offsetof(t_Object, next));
    

    The reason is that t_Object might have padding at the end, so if you copy all but the last 8 bytes, you might be copying part or all of the pointers.

    I’m not sure whether this is strictly conforming – the standard says you can copy a whole object, but I’m not sure what it says about parts of objects.

    It would be a bit strange, but imagine that this struct had some padding in the middle, and some at the end, and the implementation filled all the padding in each object with the same byte value, chosen at random, and later checked to see that the padding was still consistent. I don’t think that’s forbidden. Then this wouldn’t work.

    In practice, though, this is pretty portable. In particular, members have to appear in the order they’re defined, so offsetof(t_Object, next) certainly captures the part of the object up to (but not including) the pointers.

    If you’re able to change the structure, then you could build it up from a swapped part, and an unswapped part:

    typedef struct payload {
        signed int a[3]; 
        double float b;
        unsigned short int c[30];
    } payload;
    
    typedef struct s_Object {
        payload data;
        struct s_Object *next, *prev;
    } t_Object;
    

    Swap is then:

    payload C = pA->data;
    pA->data = pB->data;
    pB->data = C;
    

    Edit: prompted by memcpy vs assignment in C — should be memmove?

    This latter also has the advantage that it’s guaranteed by the standard to work when pA == pB. The one with memcpy isn’t, because for the second copy the regions overlap. Neither is optimal for the case of a self-swap, but generally speaking those are probably so rare that it isn’t worth checking the pointers if you don’t have to.

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

Sidebar

Related Questions

Suppose that I have a class like this class Employee: pass I create two
I have two classes which look like this in barebones form: @interface GraphNode {
So I have an instance of MyViewController in the detail view of a UISplitViewController.
Both OOD (Object-Oriented-Design) and MVC (Model-View-Controller) architectures have become staples of modern software design.
In an ajax call (for a game) that tries to pass two values in
I have ExcelStepDefinition class in which are my steps for excel testing. I have
I have created a custom MultiValue Converter to perform a bit of logic while
This one just stabbed me hard. I don't know if it's the case with
I am building an ASP.Net MVC 3 application with Entity Framework 4. When 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.