Something new for C?
Prescott K. Turner
turner at sdti.UUCP
Wed Nov 9 11:18:34 AEST 1988
In article <73 at dsoft.UUCP> root at dsoft.UUCP (Super user) writes:
>Maybe I've missed something in C, but One of the things I've never found a way
>to do and have always wanted, was a precompiler command to allow me to use
>the offset of an item into a structure.
> val = offset(test,that);
ANSI C introduces the "offsetof" macro to do just what you ask.
val = offsetof(test,that);
C++ has a closely related feature called pointers to members.
int test::*val = &test::that;
...
test x;
x.*val = 2;
Here, val is not an int, but a more strongly typed thing which can be used
only to point to offsets within a "struct test" that have type int.
"offsetof" appears simpler and more flexible to me. The only rationale I can
see for pointers to members is that they make pointers to member functions
possible. And I wonder whether it might not have been better to find a way
to use "offsetof" with member functions as well.
Sorry, I don't know which C compilers already have "offsetof".
--
Prescott K. Turner, Jr.
Software Development Technologies, Inc.
375 Dutton Rd., Sudbury, MA 01776 USA (508) 443-5779
UUCP:...genrad!mrst!sdti!turner
More information about the Comp.lang.c
mailing list