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

  • Home
  • SEARCH
  • 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 3619312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:51:46+00:00 2026-05-18T22:51:46+00:00

union test { int i; char ch; }t; int main() { t.ch=20; } Suppose

  • 0
union test
{
 int i;
 char ch;
}t;
int main()
{
 t.ch=20;
}

Suppose sizeof(int)==2 and let the memory addresses allocated for t are 2000, 2001.
Then where is 20 i.e. t.ch stored – at 2000 or 2001 or depends on endianness of machine?

  • 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-18T22:51:47+00:00Added an answer on May 18, 2026 at 10:51 pm

    The C99 standard (§6.7.2.1.14) says:

    The size of a union is sufficient to contain the largest of its members. The value of at
    most one of the members can be stored in a union object at any time. A pointer to a
    union object, suitably converted, points to each of its members
    (or if a member is a bit-
    field, then to the unit in which it resides), and vice versa.

    (emphasis added)

    The bold statement actually says that each member of the union has the same address, so they all “begin” at the same address. t, as t.ch as t.i, should be at address 2000, thus t.ch overlaps with the first byte (in address order) of t.i.

    What this means in terms of “what do I get if I try to read t.i after setting t.c” in the real world depends on platform endianness, and in facts trying to read a member of a union when you wrote in another one is Unspecified Behavior according to the C standard (§6.2.6.1.6/7, restated at §J.1.1).


    What helps more to understand the endianness of the machine (at least, I think it’s more straightforward to understand) is to have a union like this:

    union
    {
        int i;
        unsigned char ch[sizeof(i)];
    } t;
    

    doing

    t.i=20;
    

    and then looking what’s inside the two chars at t.ch. If you are on a little-endian machine you’ll get t.ch[0]==20 and t.ch[1]==0, and the opposite if you’re on a big-endian machine (if sizeof(int)==2). Notice that, as already said, this is an implementation specific detail, the standard does not even mention endianness.

    To make it even clearer: if you have a 2-byte int var set to 20, on a little-endian machine, dumping the memory associated to it in address-order, you’ll get (in hexadecimal representation, bytes split by space):

    14 00
    

    while on a big-endian machine you’ll get

    00 14
    

    The big-endian representation looks “more right” from our point of view, because in the little endian representation the bytes that make the whole int are stored in reverse order.


    Moreover I am saying that if I do this:

    int a=20;
    printf("%d",* (char*)&a);
    

    Then doesn’t the output depend on endian-ness i.e. whether 20 is stored at 2000 or 2001 ?

    Yes, here it does, but in your question you’re asking another thing; this looks more my example.

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

Sidebar

Related Questions

I have a struct: typedef struct _n { int type; union { char *s;
I am getting unexpected result for the below code. union { int aBuf[RMH_MAX_UNENCODED_LENGTH+sizeof(MSG_INFO)]; //4070+68=4138
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
Is there any way to pattern match on discriminated union functions, e.g.:- type Test
I created a simple test case: CREATE TABLE `t1` ( `id` int NOT NULL
i have a question: if i have: void test (std::vector<int> *original) { myvector =
If UNION ALL is an addition in T-SQL. What is the equivalent of subtraction?
I want to union, merge in a List that contains both references, so this
What is the difference between UNION and UNION ALL ?
I need to do a union of two tables using NHibernate and HQL. I

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.