Xref: utzoo comp.lang.c:13953 comp.lang.c++:1921 Path: utzoo!utgpu!watmath!clyde!bellcore!faline!sword!gamma!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c,comp.lang.c++ Subject: Re: Something new for C? Keywords: offset of vars within structures Message-ID: <8402@alice.UUCP> Date: 8 Nov 88 05:48:01 GMT References: <73@dsoft.UUCP> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 48 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. for example: > struct test { > long this; > int that; > char those[8]; > }; > val = offset(test,that); C++ has it -- it's called a `pointer to member'. For example: struct test { long here; // `this' is a keyword in C++ int that; char those[8]; }; Now, you can declare `testp' to be a pointer to an (unspecified) int element of an (unspecified) test structure: int test::*testp; Of course in this example, there's only one member testp could possibly point to; let's make it point there: testp = &test::that; Now, let's declare a `test' object: test t; Finally, we'll set the field of t addressed by testp to 7: t.*testp = 7; To learn many more details about member pointers, see the paper by Lippman and Stroustrup in the proceedings of the 1988 USENIX C++ conference. -- --Andrew Koenig ark@europa.att.com