declaring union

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stdvu
    New Member
    • Oct 2008
    • 21

    declaring union

    how do you declare a union with a defined union?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Say what?

    kind regards,

    Jos

    Comment

    • stdvu
      New Member
      • Oct 2008
      • 21

      #3
      lollllllz

      well i was supposed to write a code which i have written but the last statement in the exercise was something i didnt understand .... and that is

      Declare another union of the same constitution using the previously defined Union.

      Comment

      • stdvu
        New Member
        • Oct 2008
        • 21

        #4
        I was supposed to define a union as a datatype suppose i gave it a name as store now how am i suppose to declare another union with this named union??

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You mean you did something like this

          Code:
          struct MyStruct {
             int SomeData;
          } StructInstance1;
          To declare another data store all I need to do is

          Code:
          struct MyStruct StructInstance2;
          Only of course you would need to use unions.

          Comment

          • stdvu
            New Member
            • Oct 2008
            • 21

            #6
            oh ok ...... thanks!

            Comment

            • stdvu
              New Member
              • Oct 2008
              • 21

              #7
              ok so if i write it like this

              #include <stdio.h>

              typedef union
              {
              int a;
              char b;
              }data;
              union data data1;


              is it fine?

              the ques was actually this

              Define a union that can store both char and int type data values. You need to define that in a way such that later, the union name can be used as a data type itself. Declare another union of the same constitution using the previously defined Union.

              Sorry but m totally new to C and home based student so having a lot of problem studying myself ..... sorry for asking too many ques

              Comment

              • arnaudk
                Contributor
                • Sep 2007
                • 425

                #8
                Originally posted by stdvu
                define that in a way such that later, the union name can be used as a data type itself.
                The way I read it, this means you should be able to declare another union using a single type specifier. In other words, use a typedef just like you did and then you can declare more unions like this:

                data unioninstance

                (because of the typedef, "data" can be used as a type in itself and doesn't need to be preceded by the "union" keyword.)

                Comment

                • stdvu
                  New Member
                  • Oct 2008
                  • 21

                  #9
                  thanks ! :) thanks for all your help..

                  Comment

                  Working...