Pointers and Offsets and Shared Memory Concerns

Barry Margolin barmar at kulla
Wed Sep 27 09:38:28 AEST 1989


Since you're using GCC, and GCC claims to be pANS-conforming, it
should have the "offsetof" operator.  Given a structure and an element
of the structure, this will return the offset of the element into the
structure.

I'm surprised, however, that GCC complains when you try

 offset = (char *)&(sdp->ElementOfInterest) - (char *)sdp;

The operands to the - operator are both char*, and they both point
into the same object, so it should be OK to subtract them.  The pANS
defines the meaning of subtraction of pointers within the same object,
so the above should be portable.

Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list