Right now, I am writing a small project in C to test out some serialization techniques. One way that I am doing this is through the use of unions. Something that I would like to try is creating a union in the C preprocessor that holds a user-defined structure. For example, something like this:
#define create_union(name_of_structure, type_of_structure) \
\
typedef union name_of_structure { \
typeof(type_of_structure) structure; \
char buffer_that_holds_structure[sizeof(type_of_structure)]; \
} name_of_structure;
/* Usage */
struct my_data {
int id;
int other_value;
};
create_union(my_data_t, struct my_data);
/* Data type my_data_t (union my_data_t) now exists */
First of all, is this feasible? If it is, how would I go about doing this? If not, is there an alternative method I can use?
Thanks!
typeof(type_of_structure)->type_of_structure? Also, have you compiled this? Was there an error? If not, did you check with-pedantic -Wallflags to be sure?