1 2 3 4 5 6 7 8 9

typedefstruct node {
union {
int digits;
float number;
} data;
Nodeptr next;
} Node;

// struct node* next;

Having created a sample node with a declaration like
Node datanode;
which is the same as
struct node datanode;
and one can either store digits with
datanode.data.digits = 19;
or one can store a float into the same space, with
datanode.data.number = 91.3;
We can also create a node with:
Nodeptr dataptr;
dataptr = (Nodeptr) malloc ( sizeof Node);
and can set them via the pointer variable
dataptr->data.digits = 19;
or
dataptr->data.number = 91.3;


Note that what we are doing is saving a little space in the
computer (not really an overriding consideration these
days). The savings here would be small. Instead of needing
space for both an integer (digits) and a float (number),
taking 4 + 8 bytes. We can overlay them and use only Max
(4, 8) = 8 bytes (space for the longer).

February 15 1998

Page 2

TAM C201 notes