Skip to content

Instantly share code, notes, and snippets.

@jonathanlking
Last active August 29, 2015 14:22
Show Gist options
  • Save jonathanlking/4a9734336b1e98162454 to your computer and use it in GitHub Desktop.
Save jonathanlking/4a9734336b1e98162454 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef enum type
{
CHAR,
INT,
LONG
} type;
typedef struct container
{
type t;
void *val;
} container;
typedef union types
{
char c;
int i;
long l;
} types;
void printChar(container *c);
int main(int argc, char *argv[])
{
container *cont = malloc(sizeof(struct container));
types *t = malloc(sizeof(union types));
// Set the container to hold a char 'a'
t->c = 'a';
cont->t = CHAR;
cont->val = t;
printChar(cont);
free (t);
free(cont);
}
void printChar(container *cont)
{
if (cont->t != CHAR)
{
printf("Not a char!");
return;
}
char c = *((char *)(cont->val));
printf("Char: %c", c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment