K
Kris
Hi guys, been thinking on this for a while and cant see a solution.
How can you dynamically change the base type of a pointer?
Say I have to structs conveying the same information but relate to
differently mapped bit positions like these two.
struct first
{
unsigned char read : 1; /* B7 */
unsigned char write : 1;
unsigned char look : 1;
unsigned char feel : 1;
unsigned char taste : 1;
unsigned char spare : 3; /* B0 */
};
and
struct second
{
unsigned char spare : 3; /* B7 */
unsigned char write : 1;
unsigned char look : 1;
unsigned char feel : 1;
unsigned char taste : 1;
unsigned char read : 1; /* B0 */
};
What I would like to do is dynamically overlay a pointer depending on what
type I need to currently work with.
eg:
base_type ptr * current_type;
char passed_char_to_modify;
if (need_first_case)
current_type needs to be a pointer to first_case;
else
current_type needs to be a pointer to second_case;
current_type->write = 1;
If this is possible I dont have to duplicate the code for each different
type.
ie: dont need to declare two pointers, one for each and use like this:
type_1_ptr = (struct first *)(&passed_char_to_modify);
type_2_ptr = (struct second *)(&passed_char_to_modify);
if (using_type_1)
type_1_ptr->write = 1;
else
type_2_ptr->write = 1;
Can it be done?
How can you dynamically change the base type of a pointer?
Say I have to structs conveying the same information but relate to
differently mapped bit positions like these two.
struct first
{
unsigned char read : 1; /* B7 */
unsigned char write : 1;
unsigned char look : 1;
unsigned char feel : 1;
unsigned char taste : 1;
unsigned char spare : 3; /* B0 */
};
and
struct second
{
unsigned char spare : 3; /* B7 */
unsigned char write : 1;
unsigned char look : 1;
unsigned char feel : 1;
unsigned char taste : 1;
unsigned char read : 1; /* B0 */
};
What I would like to do is dynamically overlay a pointer depending on what
type I need to currently work with.
eg:
base_type ptr * current_type;
char passed_char_to_modify;
if (need_first_case)
current_type needs to be a pointer to first_case;
else
current_type needs to be a pointer to second_case;
current_type->write = 1;
If this is possible I dont have to duplicate the code for each different
type.
ie: dont need to declare two pointers, one for each and use like this:
type_1_ptr = (struct first *)(&passed_char_to_modify);
type_2_ptr = (struct second *)(&passed_char_to_modify);
if (using_type_1)
type_1_ptr->write = 1;
else
type_2_ptr->write = 1;
Can it be done?