Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/22/05 22:11
Read: times


 
#93718 - You miss the point completely...
Responding to: ???'s previous message
A union is not meant as a tool to obfusticate C source code. Nor is it really a worthwhile way to save any memory. For C coders the union is somewhat like the "variant record" of some other languages. The union in other words is designed to allow a variable, namely the union label, to hold more than one type of data. You can for example setup a union with elements for an integer, a float, and a char. Then at any one time the union can store one of these types. It is generally not with the rules of C to store one of the types in the union and to read out the value via the one of the other types. As a matter of fact a union of a particular declaration may give entirely different results if this is even attempted on different platforms. (IE an int written in may read back different things when read as a char on a PC program than it does on an 8051. Do also note that C gives no indication as to which type was last stored in the union and it is the responsibility of the programmer to keep track of this detail.

All that said it is common practice non the less to use structures to overlay data types and allow C to access the wider data types as arrays of smaller data types. For example you may see something like:
union U {
         float f_value;
         int i_value[2];
         char c_value[4];
         };


....which on platforms where a float is 4 bytes and an int is 2 bytes to be able to work with the parts of the f_value number as bytes by referencing them as the elements of the array as c_value[0], c_value[1], c_value[2], and c_value[3],

Michael Karas





List of 27 messages in thread
TopicAuthorDate
Unions in C            01/01/70 00:00      
   You miss the point completely...            01/01/70 00:00      
   Easy with Union            01/01/70 00:00      
      You can see from the Raghu example...            01/01/70 00:00      
         Platform-dependence            01/01/70 00:00      
            Padding in unions            01/01/70 00:00      
               portability            01/01/70 00:00      
      array=pointer...?            01/01/70 00:00      
         array != pointer            01/01/70 00:00      
         Quirk of C            01/01/70 00:00      
            Read the FAQ            01/01/70 00:00      
               Read the Comment            01/01/70 00:00      
                  Read everything            01/01/70 00:00      
                     Looks the same to me            01/01/70 00:00      
                        This One            01/01/70 00:00      
                        That's the problem            01/01/70 00:00      
                           Good example            01/01/70 00:00      
                              No fun            01/01/70 00:00      
                                 Well...            01/01/70 00:00      
                                 Of course it does!            01/01/70 00:00      
                                    Hmm            01/01/70 00:00      
                        Actually, even less.            01/01/70 00:00      
                           const pointer            01/01/70 00:00      
   O.K you win            01/01/70 00:00      
      Please conclude            01/01/70 00:00      
         Not Exactly            01/01/70 00:00      
         End of wrong stick?            01/01/70 00:00      

Back to Subject List