Hi All,
I have the function that need to be type cast the void point to different type of data structure. Is there any way to do that with the name of structure and the void pointer.
For example I have the follwowing data structure:

typedef struct struct_a_s
    u_int16_t      my_id;
    u_int_t          num_counter;

 } struct_a_t;

typedef struct struct_b_s
    char name[30;
    int age;
 } struct_b_t

/*
data point to data of ether struct_b_t or struct_a_t
name_structure is the name of either struct_b_t or struct_a_t

get_data(void *data, char *name_structure);
I want this macro to return the type case data to struct_a_t or struct_b_t base on the name;
Usually I did this
(struct_a_t *)data;
or (struct_b_t *)data;
for casting the data type to the correct one by if I have the macro then it is better. Just need to past in the void* and the name of structure I can cast to the correct one.
Thanks for your help.
J

Recommended Answers

All 3 Replies

I'd recommend that you reconsider your design. This kind of thing suggests that your design is awkward and ill-suited to C.

I'd recommend that you reconsider your design. This kind of thing suggests that your design is awkward and ill-suited to C.

curious what you would recommend. C++? or rethinking the entire approach and not attempting to type cast at all?

.

I'd recommend solving the problem in a different way. Faking variant types with strings is fugly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.