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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:43:27+00:00 2026-06-02T11:43:27+00:00

Empirically the following works (gcc and VC++), but is it valid and portable code?

  • 0

Empirically the following works (gcc and VC++), but is it valid and portable code?

typedef struct 
{
    int w[2];
} A;

struct B
{
    int blah[2];
};

void my_func(B b)
{
    using namespace std;
    cout << b.blah[0] << b.blah[1] << endl;
}

int main(int argc, char* argv[])
{

    using namespace std;

    A a;
    a.w[0] = 1;
    a.w[1] = 2;

    cout << a.w[0] << a.w[1] << endl;

    // my_func(a);                     // compiler error, as expected
    my_func(reinterpret_cast<B&>(a));  // reinterpret, magic?
    my_func(  *(B*)(&a) );             // is this equivalent? 

    return 0;
}
// Output:
// 12
// 12
// 12
  • Is the reinterpret_cast valid?
  • Is the C-style cast equivalent?
  • Where the intention is to have the bits located at &a interpreted as a
    type B, is this a valid / the best approach?

(Off topic: For those that want to know why I’m trying to do this, I’m dealing with two C libraries that want 128 bits of memory, and use structs with different internal names – much like the structs in my example. I don’t want memcopy, and I don’t want to hack around in the 3rd party code.)

  • 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-02T11:43:42+00:00Added an answer on June 2, 2026 at 11:43 am

    In C++11, this is fully allowed if the two types are layout-compatible, which is true for structs that are identical and have standard layout. See this answer for more details.

    You could also stick the two structs in the same union in previous versions of C++, which had some guarantees about being able to access identical data members (a “common initial sequence” of data members) in the same order for different structure types.

    In this case, yes, the C-style cast is equivalent, but reinterpret_cast is probably more idiomatic.

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

Sidebar

Related Questions

In using .NET asynchronous delegates for the first time, I was surprised to empirically
What search order does the Java compiler use to resolve Foo in the following
Does anyone have metrics on performing null test versus wrapping code in a try
Assume that methods m1 and m2 are static void and compute the same result
I have the following loop: for fileName in fileList: f = open(fileName) txt =
This is the code I'm currently running: n <- 7 N <- 52 r
Silverlight v2.0 is getting closer and closer to RTM but I have yet to
I have a few panels that I slide horizontally using the animate function that
I realize that there are similar questions , but my question is slightly different:
I'm loading a DIBSection from a file with the following: HBITMAP bmpIn = (HBITMAP)

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.