J
Jack Boot
Hello:
This came up in a code review recently, I've never heard of it before,
opinions welcome.
There is a struct that looks a bit like this
struct foo {
int a[10];
long b;
...
};
Now I have a function that starts thusly:
void
bar ( struct foo * f )
{
int * ip = (int *) foo;
...
}
The reviewer recommended, that for maximal portability I should replace
this by:
int * ip = ( (int *) ( ( (unsigned char *) foo ) + offsetOf(struct foo,
a) ) );
I've never seen this construction before, also I think type punning is
inherently non-portable so I'm not sure how much this buys you.
Cheers,
JB
This came up in a code review recently, I've never heard of it before,
opinions welcome.
There is a struct that looks a bit like this
struct foo {
int a[10];
long b;
...
};
Now I have a function that starts thusly:
void
bar ( struct foo * f )
{
int * ip = (int *) foo;
...
}
The reviewer recommended, that for maximal portability I should replace
this by:
int * ip = ( (int *) ( ( (unsigned char *) foo ) + offsetOf(struct foo,
a) ) );
I've never seen this construction before, also I think type punning is
inherently non-portable so I'm not sure how much this buys you.
Cheers,
JB