Last active
August 29, 2015 14:22
-
-
Save jonathanlking/4a9734336b1e98162454 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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