Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++

C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yaaqub
    wrote on last edited by
    #1

    What is the difference between unions and structures . Yaaqub

    N R _ 3 Replies Last reply
    0
    • Y yaaqub

      What is the difference between unions and structures . Yaaqub

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      A union is essentially a structure in which all of the fields overlay each other; you can only use one field at a time. But that's not the case with structures. In structures fields don't overlay each other and any field can be used any time. The size of a union is the maximum of the sizes of its individual members, while the size of a structure is the sum of the sizes of its members.


      Nibu thomas Software Developer

      1 Reply Last reply
      0
      • Y yaaqub

        What is the difference between unions and structures . Yaaqub

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        Each field of a union starts at the same location in memory, whereas each field in a structure gets separate memory locations.

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        1 Reply Last reply
        0
        • Y yaaqub

          What is the difference between unions and structures . Yaaqub

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          Just to explain so that you can get a clear view of what the difference is: suppose we have a structure struct abc { int a; char b; }a1; When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes and a char value of 1 byte . The total size of the structure would thus be 3 On the other hand if you use unions and declare it as union def{ int a; char ch; }u1; Then the size of operator will show 2. As the memory of variables in a union is shared and the size is of the highest occupant in the union. Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar

          D 1 Reply Last reply
          0
          • _ _AnsHUMAN_

            Just to explain so that you can get a clear view of what the difference is: suppose we have a structure struct abc { int a; char b; }a1; When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes and a char value of 1 byte . The total size of the structure would thus be 3 On the other hand if you use unions and declare it as union def{ int a; char ch; }u1; Then the size of operator will show 2. As the memory of variables in a union is shared and the size is of the highest occupant in the union. Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            AnShUmAn_VCPP wrote:

            When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes...

            I think you mean 4 bytes.


            "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

            "There is no death, only a change of worlds." - Native American Proverb

            _ 1 Reply Last reply
            0
            • D David Crow

              AnShUmAn_VCPP wrote:

              When you try to use the sizeof operator on this, the value will be correctly printed as int of size 2bytes...

              I think you mean 4 bytes.


              "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

              "There is no death, only a change of worlds." - Native American Proverb

              _ Offline
              _ Offline
              _AnsHUMAN_
              wrote on last edited by
              #6

              yes . The value that will be shown is compiler dependent. If you work on turbo c you will get 2 if you work on visula studio you will get the size of an int in this case Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar

              D 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                yes . The value that will be shown is compiler dependent. If you work on turbo c you will get 2 if you work on visula studio you will get the size of an int in this case Vision is Always important and so is your ATTITUDE. Wishes. Anshuman Dandekar

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                AnShUmAn_VCPP wrote:

                If you work on turbo c you will get 2

                But this is a Visual C++ forum...


                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                "There is no death, only a change of worlds." - Native American Proverb

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups