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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:38:15+00:00 2026-05-18T05:38:15+00:00

If I declare a Union as: union TestUnion { struct { unsigned int Num;

  • 0

If I declare a Union as:

union TestUnion
{
    struct 
    {
      unsigned int Num;
      unsigned char Name[5];
    }TestStruct;
    unsigned char Total[7];
};

Now, How can I know that whether Total[7] is used or TestStruct is used?

I am using C!
I was revisiting unions and structures and this question came to my mind.
“sizeof” can’t be used as both are of same size i.e. 7 bytes. (And Here comes another question)

When I filled only “Total” with a Character ‘a’ and Tried sizeof(TestUnionInstance), it returned 12 (Size of Char is 1 byte, Right?). So I isolated the structure from it and found that Size of Structure is 12 bytes not 5+2=7 bytes…. Strange!!
Anybody can explain??

P.S. I am using Visual Studio 2008.

  • 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-18T05:38:16+00:00Added an answer on May 18, 2026 at 5:38 am

    You can’t. That’s part of the point of unions.

    If you need to be able to tell, you can use something called a tagged union. Some languages have built-in support for these, but in C, you have to do it yourself. The idea is to include a tag along with the union which you can use to tell which version it is. Like:

    enum TestUnionTag {NUM_NAME, TOTAL};
    
    struct {
        enum TestUnionTag tag;
        union {
            struct {
                unsigned int Num;
                unsigned char Name[5];
            } TestStruct;
            unsigned char Total[7];
        } value;
    } TestUnion;
    

    Then in your code, you make sure you always set the tag to say how the union is being used.

    About the sizeof: the struct is 12 bytes because there are 4 bytes for the int (most modern compilers have a 4-byte int, the same as a long int), then three bytes of padding and five bytes for the chars (i don’t know if the padding comes before or after the chars). The padding is there so that the struct is a whole number of words long, so that everything in memory stays aligned on word boundaries. Because the struct is 12 bytes long, the union has to be 12 bytes long to hold it; the union doesn’t change size according to what’s in it.

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

Sidebar

Related Questions

DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
I had a need to declare a union inside a structure as defined below:
DECLARE @t TABLE(Words VARCHAR(100)) INSERT INTO @t SELECT 'Stack Overflow' UNION ALL SELECT 'EQUATORIAL'
declare @t table ( id int, SomeNumt int ) insert into @t select 1,10
I would like to declare a record in Delphi that contains the same layout
I have a sql query as follows: Declare @DivisionNo INT SET @DivisionNo = 5117
I am trying to make the following work: declare @ActTable as varchar(1000) declare @cPK
DECLARE @UDFLabel1 VARCHAR(50), @UDFLabel2 VARCHAR(50), @UDFLabel3 VARCHAR(50), @UDFLabel4 VARCHAR(50), @UDFLabel5 VARCHAR(50) SELECT @UDFLabel1 =
in this SQL code DECLARE @n tinyint WHILE (@n > 0) BEGIN SELECT @n
I have a table whose schema is declare @tbl table(field varchar(20)) insert into @tbl

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.