Lint question

George V. Reilly gvr at cs.brown.edu
Wed Mar 14 13:34:34 AEST 1990


Given the following code, lint complains:
	point.c(33): warning: evaluation order for ``dx'' undefined

The complaint turns out to be caused by the subexpression
	(dx *= dx)

Surely, the parenthesization and comma operators make the whole
expression well defined?

=========================================================================
typedef struct {
   int x, y;
} Point;


/* ----------------------------------------------------------------------
 * 
 *   closest_point():  Return the index of point |pt| within
 * point-table |table| of size |n|.  Return -1 on error.
 * 
 * ----------------------------------------------------------------------
 */

int
closest_point(pt, table, n)
   Point	pt;
   Point	table[];
   int		n;
{
   register int	min = MAXINT, dist, dx, dy, result = -1, x, y;

   x = pt.x; y = pt.y;
   while (--n >= 0) {
      /* Ugly but efficient test for proximity */
      if (((dx = table[n].x - x), ((dx *= dx) < min))  &&
	  ((dy = table[n].y - y), ((dist = dx + dy*dy) < min))) {
	 result = n; min = dist;
      }
   }
   return (result);
}
=========================================================================

________________
George V. Reilly			gvr at cs.brown.edu
uunet!brunix!gvr   gvr at browncs.bitnet	Box 1910, Brown U, Prov, RI 02912



More information about the Comp.lang.c mailing list