Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!nbires!isis!scicom!milehi!Ray_Gardner From: Ray_Gardner@milehi (Ray Gardner) Newsgroups: comp.lang.c Subject: pointer arithmetic question Message-ID: <0.21787448@milehi> Date: Sat, 17-Oct-87 22:40:42 EDT Article-I.D.: milehi.0.21787448 Posted: Sat Oct 17 22:40:42 1987 Date-Received: Sun, 18-Oct-87 23:38:29 EDT Organization: FidoNet node 104/56 - Mile Hi Tech, Littleton CO Lines: 36 I have coded something like this: struct s { char junk1[72]; long fpos; /* file pos. of buffer contents */ int junk2; char junk3; char buf[4097]; } *p; /* pointer to buffer structure */ char *p1; /* pointer to somewhere in p->buf */ (p1 - p->buf + p->fpos) + 101 <--- is this well-defined? This worked as I expected on a DOS compiler and a UNIX compiler, but XENIX on a TI 1100 (an 80286 machine) gave me something different. Roughly, the code generated was mov bx,_p ; pointer to structure mov ax,[bx+72] ; lsw of p->fpos add ax,_p1 ; add pointer to loc. in p->buf sub ax,_p ; subtract pointer to structure add ax,22 ; add 22 = 101 - (offset of buf) cwd ; convert word to double Is the compiler free to make this interpretation? I cured the problem with a cast: ((long)(p1 - p->buf) + p->fpos) + 101 --- * Origin: * Excalibur * Boulder CO * 303-497-6673 (Opus 1:104/39) SEEN-BY: 104/39 56 -- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- via FidoNet 104/56 Mile Hi Tech (303-973-9338) uucp: ...!{hao|isis}!scicom!milehi!oscar