Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Using Structs and Unions together effectively in C

C's Structs allow you to define a number of different variables, of different types and sizes, sequentially within a collection - you could, for example, define an animal as being:

typedef struct {
  char name[15];
   float age;
   char breed[15];
  } animal;

But what if I wanted to have several structs which were similar to each other but not identical - that had many common elements but then varied. An example might be a range of products for sale at a shop, where some of the items are sold in int units (I'll sell you one or two motorbikes, but not 1.5) and others are sold in float units (kgs of apples, for example)?

A Union allows you to define a number of different variables, of different types and sizes, in parallel - i.e. sharing the same memory - within a collection.

Used carefully, a union within a struct can be used to produce a collection that can handle a whole family of slightly varied data types, such as the motorbikes and apples that I described just above. Here are the definitions:

typedef union {
   int units;
  float kgs;
   } amount ;
typedef struct {
   char selling[15];
   float unitprice;
  int unittype;
  amount howmuch;
   } product;

So that's a product which has a description, a unit price and a type (unit sales or sales per kg). It then has an amount union, allowing the same structure to store either a float or an int as appropriate. But it IS up to the programmer to ensure that the correct use is made of the unioned element.

On the Public C Course that finished yesterday (next one in June!), I wrote a new example to show this use of a union within a structure, and you can see the complete code here (opens in a new window).

You'll note the use of an array of pointers to product structs to allow me to loop through all the products easily, but the the great care I have to take within the loop to ensure that I use the integer or float member of the union as appropriate, based on the unittype variable I have provided for the purpose.

It's no co-incidence that there a great similarity here between the varied members of a combined structure / union and the varied data members of a series of classes that are all inherited from the same base class in C++, Java, etc - indeed, you could describe the use of structs and unions that I talk about here as a do-it-yourself form of inheritance and polymorphism.
(written 2008-03-21, updated 2008-03-22)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C209 - C and C based languages - Structures and Unions
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
  [1669] What are Unions (C programming) - (2008-06-08)
  [2573] C Structs - what, how and why - (2010-01-13)
  [3122] When is a program complete? - (2011-01-06)
  [3145] Structures v Structure Pointers in C. How, which, why. - (2011-01-25)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)

Back to
Businesses in West Wiltshire - networking
Previous and next
Horse's mouth home
Forward to
Well House Consultants / Manor - Staff
Some other Articles
String interpretation in Ruby
Some Ruby programming examples from our course
Variable types in Ruby
Well House Consultants / Manor - Staff
Using Structs and Unions together effectively in C
Businesses in West Wiltshire - networking
Ruby, C, Java and more - getting out of loops
What is an lvalue? (Perl, C)
Congratulations, Martin and Marta
Rome, and the faith of Rome
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1584_Usi ... -in-C.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb