1 2 3 4 5 6 7 8 9

Once the item has been stored in datanode it can be
retrieved by first interrogating the TAG field. See King P.
345-350 or K&R P. 148


What now if you wanted an array of such a variant record
structure?
(As indeed you might for the second assignment!)


typedef struct node* Nodeptr;


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


At this stage we have only a definition of a sample element
in our array. If we wanted to create M of them we would
first need to declare a pointer to such an element


Nodeptr np;
then get space for the array with
np = (Nodeptr) malloc (M*sizeof(Node));
or
np = (Nodeptr) calloc (M, sizeof(Node));


To access the zero'th TAG field we could write
kind = np->TAG;


while the i'th tag:

February 15 1998

Page 4

TAM C201 notes