0

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

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Narue
0

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

0

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?

.

Edited by jephthah: n/a

1

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.