Something new for C?
Andrew Koenig
ark at alice.UUCP
Tue Nov 8 15:48:01 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. 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 at europa.att.com
More information about the Comp.lang.c
mailing list