Path: utzoo!attcan!uunet!jarthur!nntp-server.caltech.edu!seismo.gps.caltech.edu!bruce From: bruce@seismo.gps.caltech.edu (Bruce Worden) Newsgroups: comp.lang.c Subject: Re: Order of evalution of expressions. Message-ID: <1990Sep25.000621.6980@nntp-server.caltech.edu> Date: 25 Sep 90 00:06:21 GMT References: <6398@castle.ed.ac.uk> Sender: bruce@seismo.gps.caltech.edu (Bruce Worden) Organization: California Institute of Technology, CA Lines: 33 Nntp-Posting-Host: sis.gps.caltech.edu In article <6398@castle.ed.ac.uk> elee24@castle.ed.ac.uk (H Bruce) writes: >Is the line > >value = *ptr - *ptr++; > >sensible C ? As others have pointed out, it is not; the behavior is undefined and therefore may be implementation dependent. >If not what is the fastest way of computing this type of expression ? >Would the following lines be optimized by a compiler (so that value is >not loaded twice) ? >value = *ptr; >value -= *ptr++; Remember, the increment takes place after the value is used, so you are effectively subtracting *ptr from itself. I suspect that what you want to do is: value = ptr[0] - ptr[1]; ptr++; As was discussed here a couple of weeks ago, this form should be as efficient as the form: value = *ptr - *(ptr+1); ptr++; The first form is, I think, clearer. -------------------------------------------------------------------------- C. Bruce Worden bruce@seismo.gps.caltech.edu 252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125