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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:55:10+00:00 2026-05-14T21:55:10+00:00

I read about unions the other day( today ) and tried the sample functions

  • 0

I read about unions the other day( today ) and tried the sample functions that came with them. Easy enough, but the result was clear and utter garbage.

The first example is:

union Test
{ 
    int Int;

    struct
    {
        char byte1;
        char byte2;
        char byte3;
        char byte4;
    } Bytes;
};

where an int is assumed to have 32 bits. After I set a value Test t; t.Int = 7; and then cout

cout << t.Bytes.byte1 << etc...

the individual bytes, there is nothing displayed, but my computer beeps. Which is fairly odd I guess.

The second example gave me even worse results.

union SwitchEndian
{
    unsigned short word;

    struct
    {
        unsigned char hi;
        unsigned char lo;
    } data;
} Switcher; 

Looks a little wonky in my opinion. Anyway, from the description it says, this should automatically store the result in a high/little endian format when I set the value like

Switcher.word = 7656; and calling with cout << Switcher.data.hi << endl

The result of this were symbols not even defined in the ASCII chart. Not sure why those are showing up.

Finally, I had an error when I tried correcting the example by, instead of placing Bytes at the end of the struct, positioning it right next to it. So instead of

struct {} Bytes;

I wanted to write

struct Bytes {};

This tossed me a big ol’ error. What’s the difference between these? Since C++ cannot have unnamed structs it seemed, at the time, pretty obvious that the Bytes positioned at the beginning and at the end are the things that name it. Except no, that’s not the entire answer I guess. What is it then?

  • 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-14T21:55:11+00:00Added an answer on May 14, 2026 at 9:55 pm

    Note that, technically, reading from a member of a union other than the member that was last written to results in undefined behavior, so if you last assigned a value to Int, you cannot read a value from Bytes (there’s some discussion of this on StackOverflow, for example, in this answer to another question).

    Chris Schmich gives a good explanation of why you are hearing beeps and seeing control characters, so I won’t repeat that.

    For your final question, struct {} Bytes; declares an instance named Bytes of an unnamed struct. It is similar to saying:

    struct BytesType {};
    BytesType Bytes;
    

    except that you cannot refer to BytesType elsewhere. struct Bytes {}; defines a struct named Bytes but declares no instances of it.

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

Sidebar

Related Questions

I read about improving site perforamance and tried to use some of them, but
Read about the z-index issue and tried anything, but just can't seem to fix
I read about a string/word that did so the Routing crashed but I can't
I read about the invalidate overload that takes a rectangle, but I didn't understand
I read about an inheritance feature in PostgreSQL that seemed pretty neat. Unfortunately I
I read about ContactsContract.CommonDataKinds.GroupMembership , but I can't figure out what URI use to
Read this question today about safe and unsafe code I then read about it
I read about structure in c++ that it can not contain instance of itself.
I read about JSON from internet but still i have not got the grasp
I read about __packed__ from here and, I understood that when __packed__ is used

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.