I have the sturct token_t
down below. It has two things in it.
- type (enum)
- value (union)
The reason why value is union, cz a token can be either a number or string.
The main problem is that I cannot design a proper function that can print this token out for me. Check out my void print_token(token_t* token)
function. I know it's terrible. Just becase the printf()
prints only either %d (decimal) or %s (string) I cannot print my token out. I want my function to print the token no matter what the value is.
// ===========================================================================
typedef enum token_type_t
{
ID, // Identifier
STR, // String
WHS, // Whitespace
LPR, // Left Parenthesis
RPR, // Right Parenthesis
LCB, // Left Curly Bracket
RCB, // Right Curly Bracket
LSB, // Left Square Bracket
RSB, // Left Square Bracket
EOF, // End Of File
EQL, // Equal Sign
SEM, // Semicolon
} token_type_t;
typedef union token_value_t
{
char* str_value;
int int_value;
} token_value_t;
typedef struct token_t
{
token_type_t type;
token_value_t value;
} token_t;
// ===========================================================================
// Here's the problem. I want this function to print the token no matter what type
// the value is, I want it to be dynamic. Is there any way to do that?
void print_token(token_t* token)
{
printf("Token { type: %d, value: %s }\n", token->type, token->value);
}