Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!kth!sunic!dkuug!freja!njk From: njk@freja.diku.dk (Niels J|rgen Kruse) Newsgroups: comp.lang.c Subject: Re: Adding two pointers Summary: Whats an array? Message-ID: <4646@freja.diku.dk> Date: 6 May 89 16:24:14 GMT References: <2765@buengc.BU.EDU> <563@lzaz.ATT.COM> <4093@ficc.uu.net> <17340@mimsy.UUCP> Organization: DIKU, U of Copenhagen, DK Lines: 33 In article <17340@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes: > > But subtraction of pointers is a common occurrence in C, and any > optimising compiler worth its name should note this. Subtraction of > two pointers is only defined if both point to elements of the same > array; and when they do, it is guaranteed that the difference is a > whole number of elements, or in other words a proper multiple of `size'. The word "array" as used when saying that two pointers must point into the same array in order to subtract them is a bit foggy to me. What about struct { char c; double d; } x[10]; Are (& x[3].d) and (& x[7].d) not pointing into x? Is (& x[3].d - x[7].d) undefined? What array is (char *)x pointing into? I seem to remember to have seen statements in this newsgroup by people enjoying gurustatus, that dpANS C provides a way to get offsets of fields within structs. What would be the use of that if objects like x are not flat? -- Niels J|rgen Kruse Email njk@diku.dk Mail Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark