I want to create a linked list, but I want the data field to be either int or float. I wrote some code like the following:
union int_or_float
{
int int_member;
float float_member;
};
struct node
{
union int_or_float data;
struct node *next;
};
I want to write stuff like:
typedef struct node item;
but how to specify which type in union do I like item to be?
That’s done automatically when you access the data. A union is nothing more than a slightly-less-kludgy way of casting a pointer.
This will output
run/tested on an Intel Mac with gcc version 4.2.1 (Apple Inc. build 5659)