Xref: utzoo comp.lang.c:13934 comp.lang.c++:1915 Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!bloom-beacon!bu-cs!mirror!necntc!necis!mrst!sdti!turner From: turner@sdti.UUCP (Prescott K. Turner) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: Something new for C? Summary: offsetof Keywords: offset structures Message-ID: <326@sdti.UUCP> Date: 9 Nov 88 01:18:34 GMT References: <73@dsoft.UUCP> Reply-To: turner@sdti.UUCP (0006-Prescott K. Turner, Jr.) Organization: Software Development Technologies, Sudbury MA Lines: 27 In article <73@dsoft.UUCP> root@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