|
|
1.1 ! root 1: ! 2: ! 3: union C Keyword union ! 4: ! 5: ! 6: ! 7: ! 8: Multiply declare a variable ! 9: ! 10: ! 11: A union defines an area of storage that can accept any one of ! 12: several types of data. In effect, it is a multiple declaration ! 13: of a variable. For example, a union may be declared to consist ! 14: of an int, a double, and a char *. Any one of these three ! 15: elements can be held by the union at a time, and will be handled ! 16: appropriately by it. For example, the declaration ! 17: ! 18: ! 19: union { ! 20: int number; ! 21: double bignumber; ! 22: char *stringptr; ! 23: } example; ! 24: ! 25: ! 26: allows example to hold either an iinntt, a ddoouubbllee, or a pointer to a ! 27: cchhaarr, whichever is needed at the time. All of these have the ! 28: same address. The elements of a uunniioonn are accessed like those of ! 29: a ssttrruucctt: for example, to access nnuummbbeerr from the above example, ! 30: type eexxaammppllee.nnuummbbeerr. ! 31: ! 32: unions are helpful in dealing with heterogeneous data, especially ! 33: within structures; however, you are responsible for keeping track ! 34: of what data type the union is holding at any given time. Pas- ! 35: sing a double to a union and then reading the union as though it ! 36: held an int will yield results that are unpredictable, and ! 37: probably unwelcome. ! 38: ! 39: ***** Example ***** ! 40: ! 41: For an example of how to use a union in a program, see the entry ! 42: for byte ordering. ! 43: ! 44: ***** See Also ***** ! 45: ! 46: C keywords, struct, structure ! 47: ! 48: ! 49: ! 50: ! 51: ! 52: ! 53: ! 54: ! 55: ! 56: ! 57: ! 58: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.