|
|||||||||||
|
Re: hmm
From: Thomas David Rivers <ponds!rivers(at)dg-rtp.dg.com>
Date: Sun Feb 23 1997 - 20:01:57 EST
It may not be too clear... but, and at the risk of repeating what other's may have indicated - the C language, by definition, evaluates this from left to right. And, in the situation above (a logical-and) the first 0-valued expression will cause the result of the logical-and to be 0. Furthermore, the a compiler, or optimizer, is forbidden from evaluating the rest of the expression. That is, if *p is equal to '\0' - the remainder of the logical-and will not be evaluated... if an optimizer does cause this to happen, the compiler/optimizer is in error. So, if strlen(p) was 0, it wouldn't matter; p[-1] would never be referenced. In this veign you'll also often see code like: if(p != NULL && !strcmp(p,"some value") ) the left side of logical-and guarantees that the pointer 'p' is not NULL. The right side of the logical-and can then safely dereference 'p'. Again; this isn't a coincedence, C is defined (from the early K&R days) to behave in this manner. In compiler terms it's called short-circuit evaluation of logical expressions...
This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 12:41:03 EDT |
||||||||||
|
|||||||||||