Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!mcnc!rti!xyzzy!throopw From: throopw@xyzzy.UUCP (Wayne A. Throop) Newsgroups: comp.lang.c Subject: Re: pointer alignment when int != char * Message-ID: <235@xyzzy.UUCP> Date: Mon, 31-Aug-87 10:32:26 EDT Article-I.D.: xyzzy.235 Posted: Mon Aug 31 10:32:26 1987 Date-Received: Fri, 4-Sep-87 05:52:49 EDT References: <493@its63b.ed.ac.uk> <6061@brl-smoke.ARPA> <3812@spool.WISC.EDU> <483@mtxinu.UUCP> <129@minya.UUCP> <6359@brl-smoke.ARPA> Organization: Data General, RTP NC. Lines: 25 > gwyn@brl-smoke.ARPA (Doug Gwyn ) > Here is how to find the difference (in number of bytes) of two > pointers in various circumstances: > If p1 and p2 point to objects of the SAME type > (which must be members of the same aggregate): > (p2 - p1) / (int)sizeof(type) Um... no, I don't think so. Analyzing the units here, we see that (p2-p1) has units "objects", and sizeof(type) has units "bytes/object". So this proposed formula has units "objects^2/byte", not "bytes" as required. It should be (p2-p1)*(int)sizeof(type). > If p1 and p2 point to objects of DIFFERENT types > (which must be members of the same aggregate): > ((char *)p2 - (char *)p1) / (int)sizeof(char) This is more like it, but as Doug notes, the division is superfluous according to draft X3J11 (though your mileage may vary). Note that this also works if p2 and p1 point to objects of the same type. -- The meaning of Stonehenge in Tralfamadorian, when viewed from above, is: "Replacement part being rushed with all possible speed." --- Kurt Vonnegut, Jr.; Sirens of Titan. -- Wayne Throop !mcnc!rti!xyzzy!throopw