Union declaration

This is a discussion on Union declaration within the C Programming forums, part of the General Programming Boards category; Q1. Code: union uv { unsigned long vol; unsigned char vol_bricks[4]; }; xdata union uv xv[3]; Is the above declaration ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    Union declaration

    Q1.
    Code:
    union uv
    {
    	unsigned long vol;
    	unsigned char vol_bricks[4];
    };
    xdata union uv xv[3];
    Is the above declaration "union uv xv[3];" valid??
    Can I declare array of unions as above??


    2)
    Code:
    void f1()
    {
          union uv biggest_value;
    
          xv[0].vol = value0;
          xv[0].vol = value0;
          xv[0].vol = value0;
    
          biggest_value.vol = xv[0].vol;
    
          if(  biggest_value.vol_bricks[0] < xv[1].vol_bricks[0] )  
                 biggest_value.vol = xv[1].vol;
    
          if(  biggest_value.vol_bricks[0] < xv[1].vol_bricks[0] )
                 biggest_value.vol = xv[1].vol;
    
          Global_value =  biggest_value.vol;
    }
    HERE:
    "if( biggest_value.vol_bricks[0] < xv[1].vol_bricks[0] )"
    "if( biggest_value.vol_bricks[0] < xv[1].vol_bricks[0] )"

    these "if" statements are observed, they are never true.
    Is there any error in declaration?
    Is the way "character item 0" in the unoins are compared causing any problem?

    3.I require guidance regarding usage of UNION.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
          xv[0].vol = value0;
          xv[0].vol = value0;
          xv[0].vol = value0;
    These lines are all redundant. You only need one. The same goes for your if check. There's no point in having more than one.

    Aside from the xdata portion you have typed in there, sure, it's correct. This is perfectly valid, and will declare an array of unions:
    Code:
    union foo
    {
        ...some stuff...
    };
    
    union foo bar[5];  /* create an array, called bar of 5 'foo' unions. */
    You can compare and use union members the same way you would use struct members. If you know how to use a struct, you know how to use a union. The only difference is that all members of the same union share the same space in memory. If you change the value of one member, it affects them all.
    Code:
    union foo
    {
        char c;
        int i;
        double d;
        float f;
    };
    
    union foo bar;
    
    bar.c = 'a';
    Now you've just modified all members of the union, since they share the same space in memory.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21