Changing the base of a pointer

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?
 
E

Eric Sosman

Kris said:
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?

If I understand your intent, no. Every operator in a C
program (remember, = is an operator) uses operands whose types
are determined at compile time; you can't change the types (and
the code they cause to be generated) at run time.
 
J

jacob navia

Kris said:
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?

Why don't you make a union of the two structures?
Then you can access both structures by accessing the two types
in the union, all with one pointer

jacob
 
J

jacob navia

Eric said:
If I understand your intent, no. Every operator in a C
program (remember, = is an operator) uses operands whose types
are determined at compile time; you can't change the types (and
the code they cause to be generated) at run time.

Why can't he make a union Eric?

union u {
structure first {
// ...
}f;
structure second {
// ...
}s;
};
 
E

Eric Sosman

jacob navia wrote On 08/02/06 09:54,:
Eric said:
Kris wrote:

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?
[...]

If I understand your intent, no. Every operator in a C
program (remember, = is an operator) uses operands whose types
are determined at compile time; you can't change the types (and
the code they cause to be generated) at run time.

Why can't he make a union Eric?

union u {
structure first {
// ...
}f;
structure second {
// ...
}s;
};

Yes, he can make a union -- but I don't think that
addresses what he's trying to do, which seems to be "Use
the same line of source code to access identically-named
but different members of two different struct types:"

magicptr->elementname = 42;

.... where "elementname" appears in both structs (with
different declarations) and "magicptr" is somehow manipulated
to know which of the two struct types it happens to reference
at the moment.

At least, that's what his example looked like to me.
I may have misunderstood what he was after, of course.
 
K

Kris

Eric Sosman said:
jacob navia wrote On 08/02/06 09:54,:
Eric said:
Kris wrote:


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?
[...]

If I understand your intent, no. Every operator in a C
program (remember, = is an operator) uses operands whose types
are determined at compile time; you can't change the types (and
the code they cause to be generated) at run time.

Why can't he make a union Eric?

union u {
structure first {
// ...
}f;
structure second {
// ...
}s;
};

Yes, he can make a union -- but I don't think that
addresses what he's trying to do, which seems to be "Use
the same line of source code to access identically-named
but different members of two different struct types:"

magicptr->elementname = 42;

... where "elementname" appears in both structs (with
different declarations) and "magicptr" is somehow manipulated
to know which of the two struct types it happens to reference
at the moment.

Yep, that was it.
Thanks for the thoughts.
Ended up duplicating the source for now but am still thinking of a solution.
Will get it one day
:)
 
E

Eric Sosman

Kris said:
Yep, that was it.
Thanks for the thoughts.
Ended up duplicating the source for now but am still thinking of a solution.
Will get it one day

I don't think you will -- not in C, anyhow. The fact that
two different things are called "elementname" in their own
contexts does not mean they are interchangeable. (I know
several people named "Kris," of both genders -- and they are
most definitely *not* accessible via the same expression!)

The closest I think you can come would be to write a macro
once and expand it twice. That's probably more work than it's
worth -- if you expanded it half a dozen times it might be worth
while, but ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top