S
Shao Miller
A possibly interesting/curious use of the ternary conditional
operator...
The only thing I could find to do with it was the non-portable:
#include <stddef.h>
#define NULL_OF_TYPE(x) \
(1 ? 0 : &(x))
#define offsetof2(id, member) \
((size_t)&(NULL_OF_TYPE(id)->member))
int main(void) {
struct {
double bar;
long baz;
} foo;
return (int)offsetof2(foo, baz);
}
This 'offsetof2' macro doesn't take 'struct XXX' as its first
argument, but an actual lvalue of a struct type.
A fellow C programmer had suggested a look here[1][*], which is pretty
loose inspiration.
Enjoy (for what it's worth).
- Shao Miller
[1] http://www.artima.com/cppsource/foreach.html
[*] It's C++
operator...
The only thing I could find to do with it was the non-portable:
#include <stddef.h>
#define NULL_OF_TYPE(x) \
(1 ? 0 : &(x))
#define offsetof2(id, member) \
((size_t)&(NULL_OF_TYPE(id)->member))
int main(void) {
struct {
double bar;
long baz;
} foo;
return (int)offsetof2(foo, baz);
}
This 'offsetof2' macro doesn't take 'struct XXX' as its first
argument, but an actual lvalue of a struct type.
A fellow C programmer had suggested a look here[1][*], which is pretty
loose inspiration.
Enjoy (for what it's worth).
- Shao Miller
[1] http://www.artima.com/cppsource/foreach.html
[*] It's C++