create new tag
, view all tags
from Structures and Unions:

A structure is an instance of a sequence of named data types collected into a template - analogous to a Pascal record. A union is a data type that is similar to a structure but at most will contain only on member of its aggregation - it is handy for declaring a variable that may contain different types at different times. Structures and unions provide the way of extending the number of data types available.

I think the above is a reasonable definition, but we must be careful as the quote is from a document on a variation of C, EIC (Extensible Interactive C), and some differences with "standard" C may exist.




Simple example (from Data Programs and Structures, Christopher J. Van Wyk, 1992):

union u {
   char c[4];
   int n;
   float x;
} uvar;

A variable uvar that contains 4 bytes that can be interpreted as an array of 4 chars, a single (32 bit) int, or a single (32 bit) float(ing point number).

What does the "u" do? (Is it a required but useless "implicit" typedef? — could I create another instance of the union with a statement (declaration) like u uvar2;? Is the "u" necessary?


Accessing uvar as:

  • an array of 4 chars uvar.c(0..3)??
  • an integer uvar.n
  • a float uvar.x

Oops, maybe I was thinking about a different programming language — I thought that by storing an int (uvar.n) in uvar, and then accessing it as an array of chars (uvar.c(0..3), I could get the four individual bytes that make up the integer. The book I'm reading says the result is undefined (so maybe it works with some compilers, but is not guaranteed to work with all?).

A union will require (reserve) storage for its largest possible "type".

A more complex example (same book):

struct circdata {
   point center;
   double radius;

struct rectdata {
   point sw, ne;

typedef struct figure figure;
struct figure {
   figure *next;
   int kind;
   union fig {
      struct circdata circle;
      struct rectdata rectangle;
   } fig;

Aside: "given a pointer p to a figure whose kind was circle, we would have to write p->fig.circle.radius to access its radius."

An annoying thing about the previous example is reflected in the line typedef struct figure figure; — what are the two "figure"s? (And, likewise, later the two "fig"s?) On the other hand, is it really important, except to a pedant? Well, yes, it seems that I would have a clearer understanding. Is there a chance the author of the book didn't have a crystal clear understanding? (No offense intended.)

I'm rewriting the example to provide unique names for each to make it more clear what each serves — this is an attempt but may be incorrect:

The same example, with some identifiers changed and comments added:

struct circdata {
   point center;
   double radius;

struct rectdata {
   point sw, ne;

typedef struct figure figure1; /*Define a type, figure1, that is a struct, of type figure??*/
struct figure { /*Definition for the struct figure?? */
   figure next; /*Should this be figure or figure 1 — an instance of  figure or figure1??*/
   int kind;
   union fig { /*fig is the "implicit" type identifier for this union (it is not a variable of type fig)?? */
      struct circdata circle;
      struct rectdata rectangle;
   } fig1; /*fig1 is a variable, an instance of the union defined in the previous lines??*/


See Resource Recommendations. Feel free to add additional resources to these lists, but please follow the guidelines on ResourceRecommendations including Guidelines_for_Rating_Resources.


  • (rhk) [[][]]; ; —


  • () RandyKramer - 23 Apr 2003
  • If you edit this page: add your name here; move this to the next line; and if you've used a comment marker (your initials in parenthesis), include it before your WikiName.

Page Ratings

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2003-04-23 - RandyKramer
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by PerlCopyright 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WikiLearn? WebBottomBar">Send feedback
See TWiki's New Look