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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:15:35+00:00 2026-05-11T00:15:35+00:00

Is there any good example to give the difference between a struct and a

  • 0

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?

  • 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. 2026-05-11T00:15:36+00:00Added an answer on May 11, 2026 at 12:15 am

    With a union, you’re only supposed to use one of the elements, because they’re all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.

    To give a concrete example of their use, I was working on a Scheme interpreter a little while ago and I was essentially overlaying the Scheme data types onto the C data types. This involved storing in a struct an enum indicating the type of value and a union to store that value.

    union foo {   int a;   // can't use both a and b at once   char b; } foo;  struct bar {   int a;   // can use both a and b simultaneously   char b; } bar;  union foo x; x.a = 3; // OK x.b = 'c'; // NO! this affects the value of x.a!  struct bar y; y.a = 3; // OK y.b = 'c'; // OK 

    edit: If you’re wondering what setting x.b to ‘c’ changes the value of x.a to, technically speaking it’s undefined. On most modern machines a char is 1 byte and an int is 4 bytes, so giving x.b the value ‘c’ also gives the first byte of x.a that same value:

    union foo x; x.a = 3; x.b = 'c'; printf('%i, %i\n', x.a, x.b); 

    prints

    99, 99 

    Why are the two values the same? Because the last 3 bytes of the int 3 are all zero, so it’s also read as 99. If we put in a larger number for x.a, you’ll see that this is not always the case:

    union foo x; x.a = 387439; x.b = 'c'; printf('%i, %i\n', x.a, x.b); 

    prints

    387427, 99 

    To get a closer look at the actual memory values, let’s set and print out the values in hex:

    union foo x; x.a = 0xDEADBEEF; x.b = 0x22; printf('%x, %x\n', x.a, x.b); 

    prints

    deadbe22, 22 

    You can clearly see where the 0x22 overwrote the 0xEF.

    BUT

    In C, the order of bytes in an int are not defined. This program overwrote the 0xEF with 0x22 on my Mac, but there are other platforms where it would overwrite the 0xDE instead because the order of the bytes that make up the int were reversed. Therefore, when writing a program, you should never rely on the behavior of overwriting specific data in a union because it’s not portable.

    For more reading on the ordering of bytes, check out endianness.

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

Sidebar

Ask A Question

Stats

  • Questions 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer First and foremost, open ~/.gdbinit (that's the file called .gdbinit… May 11, 2026 at 3:06 pm
  • added an answer Wow, where do I begin. Ok, I fixed up your… May 11, 2026 at 3:06 pm
  • added an answer Contact your hosting provider and complain. Often with web hosting,… May 11, 2026 at 3:06 pm

Related Questions

I have always written regexes like this <A HREF=([^]*) TARGET=_blank>([^<]*)</A> but I just learned
Okay, here's one for the pro's: For a couple of years now, i've been
Modern computers have more and more cores. We want to change our current linear
Background Given that 'most' developers are Business application developers, the features of our favorite

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.