#include <stdio.h>
#include <stdlib.h>
/* structure type flag values */
#define TYPENONE 0 /* Not pointing at an object */
#define TYPECHAR 1 /* char */
#define TYPEINT 2 /* int */
#define TYPELONG 3 /* long */
#define TYPEDOUBLE 4 /* double */
struct node {
struct node *pfwd; /* forward ptr */
struct node *pbwd; /* backward ptr */
void *pobject; /* ptr to object */
unsigned int objtype; /* indicate object type */
};
main()
{
char c = 'A';
int i = 10;
long int 1 = 123456;
double d = 123.45;
struct node *pnode;
pnode = malloc(sizeof(struct node));
/* let's point to a double */
pnode->pobject = &d;
pnode->objtype = TYPEDOUBLE;
pnode->pfwd = NULL;
pnode->pbwd = NULL;
/* at a later point, let's process the object to which we point */
switch (pnode->objtype) {
case TYPECHAR:
printf ("char: %c\n", *(char *)pnode->pobject);
break;
case TYPEINT:
printf("int: %d\n", *(int *)pnode->pobject);
break;
case TYPELONG:
printf("long: %ld\n", *(long *)pnode->pobject);
break;
case TYPEDOUBLE:
printf("double: %f\n", *(double *)pnode->pobject);
break;
case TYPENONE:
printf ("none:\n");
break;
}
}
The output generated by this program is:
double: 123.450000