« Talk on TransWilts train service to Green Party | Main | Dynamic Memory Allocation in C »
June 08, 2008
What are Unions (C programming)
Structures are used to hold a whole lot of different pieces of data in a collection - by element name not number. Sometimes, you’ll only want to hold some elements of your structure in specific circumstances - a good example is where you have a whole lot of similar things you’re describing, but only a limited range of elements applied to each.
A very good example in real ’system’ programming is an X Windows event - each has a time stamp and a sequence number, and an event type, and each has a target window. But only some of them have X and Y co-ordinates, only some of them have keypresses and statuses, and so on. One possible way of holding such events would be a sparsely populated structure, but in practise this would be very wasteful of memory - so a union is used.
Posted by gje at June 8, 2008 03:09 PM