Difference between Unions and Structures...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravikiran

    Difference between Unions and Structures...

    Hi,

    I want know the differences between Unions and Structures in C
    programming.

    Thank you..
  • Pawel Dziepak

    #2
    Re: Difference between Unions and Structures...

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Ravikiran wrote:
    I want know the differences between Unions and Structures in C
    programming.
    The size of union is the size of its largest member. In most
    implementations (but standard does *not* require it) all members of
    union are placed at the same location in memory.
    Structures are just "normal" groups of variables.

    Pawel Dziepak

    PS I really shouldn't help you that way, there is a lot of information
    about structures and unions in many FAQs, articles, etc.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iEYEARECAAYFAkk O3r4ACgkQPFW+cU iIHNp5SwCgsATEi HUwMyAd4dGgVJQQ AUR2
    MCoAn1+6G5wqv5h 6JBxjP3x++K5SDB LT
    =z5G+
    -----END PGP SIGNATURE-----

    Comment

    • Martien Verbruggen

      #3
      Re: Difference between Unions and Structures...

      On Mon, 03 Nov 2008 12:21:34 +0100,
      Pawel Dziepak <pdziepak@quarn os.orgwrote:
      >
      Ravikiran wrote:
      >I want know the differences between Unions and Structures in C
      >programming.
      >
      The size of union is the size of its largest member. In most
      implementations (but standard does *not* require it) all members of
      union are placed at the same location in memory.
      From 6.2.5 - 20

      — A union type describes an overlapping nonempty set of member
      objects, each of which has an optionally specified name and possibly
      distinct type.

      Also, from 6.5.8 - 5

      All pointers to members of the same union object compare equal.

      I'd say that pretty much gurantees that the members of a union, as you
      say it, are placed at the same location in memory.

      Martien

      PS. quotes from n1256. Wording in actual c99 standard is identical. I
      don't own a c89 copy, but in a draft for that standard, similar ior
      identical wording for the above quoted is present

      --
      |
      Martien Verbruggen | Since light travels faster than sound, is
      | that why some people appear bright until you
      | hear them speak?

      Comment

      • gk

        #4
        Re: Difference between Unions and Structures...

        On Nov 3, 6:06 pm, Martien Verbruggen <m...@heliotrop e.com.auwrote:
        On Mon, 03 Nov 2008 12:21:34 +0100,
                Pawel Dziepak <pdzie...@quarn os.orgwrote:
        >
        >
        >
        Ravikiran wrote:
        I want know the differences between Unions and Structures in C
        programming.
        >
        The size of union is the size of its largest member. In most
        implementations (but standard does *not* require it) all members of
        union are placed at the same location in memory.
        >
        From 6.2.5 - 20
        >
          — A union type describes an overlapping nonempty set of member
            objects, each of which has an optionally specified name and possibly
            distinct type.
        >
        Also, from 6.5.8 - 5
        >
          All pointers to members of the same union object compare equal.
        >
        I'd say that pretty much gurantees that the members of a union, as you
        say it, are placed at the same location in memory.
        >
        Martien
        >
        PS. quotes from n1256. Wording in actual c99 standard is identical. I
        don't own a c89 copy, but in a draft for that standard, similar ior
        identical wording for the above quoted is present
        >
        --
                                |
        Martien Verbruggen      | Since light travels faster than sound, is
                                | that why some people appear bright until you
                                | hear them speak?
        the difference basically arises in the memory allocation..... while
        structure allocates memory separately to each of its members, the same
        is not the case with unions..it allocates memory only to the largest
        available member...the rest of them are allocated within this block of
        memory only

        Comment

        • Pawel Dziepak

          #5
          Re: Difference between Unions and Structures...

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Martien Verbruggen wrote:
          I'd say that pretty much gurantees that the members of a union, as you
          say it, are placed at the same location in memory.
          Sorry, it was my fault. I heard that the location can be different, but
          I didn't check that. Anyway, sorry for providing incorrect information.

          Pawel Dziepak
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.9 (GNU/Linux)
          Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

          iEYEARECAAYFAkk PB1cACgkQPFW+cU iIHNpcSgCeMdASw 61Y1oIZvce1+NR1 2WXA
          xcsAn0jYzPyCUAK QsZmOnjbQydXs0M 2P
          =xpZ8
          -----END PGP SIGNATURE-----

          Comment

          • Keith Thompson

            #6
            Re: Difference between Unions and Structures...

            Ravikiran <ravisattigeri@ gmail.comwrites :
            I want know the differences between Unions and Structures in C
            programming.
            If your textbook doesn't explain this clearly, you need a new
            textbook. I recommend K&R2 (Kernighan & Ritchie, The C Programming
            Language, 2nd Edition).

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • CBFalconer

              #7
              Re: Difference between Unions and Structures...

              Ravikiran wrote:
              >
              I want know the differences between Unions and Structures in C
              programming.
              Try the C standard. For example:

              6.7.2.1 Structure and union specifiers

              ...

              Semantics

              [#4] As discussed in 6.2.5, a structure is a type consisting
              of a sequence of members, whose storage is allocated in an
              ordered sequence, and a union is a type consisting of a
              sequence of members whose storage overlap.

              and a good deal more.

              Some useful references about C:
              <http://www.ungerhu.com/jxh/clc.welcome.txt >
              <http://c-faq.com/ (C-faq)
              <http://benpfaff.org/writings/clc/off-topic.html>
              <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf(C99)
              <http://cbfalconer.home .att.net/download/n869_txt.bz2 (pre-C99)
              <http://www.dinkumware. com/c99.aspx (C-library}
              <http://gcc.gnu.org/onlinedocs/ (GNU docs)
              <http://clc-wiki.net/wiki/C_community:com p.lang.c:Introd uction>

              --
              [mail]: Chuck F (cbfalconer at maineline dot net)
              [page]: <http://cbfalconer.home .att.net>
              Try the download section.

              Comment

              Working...