Considering the following two code fragments,
(1)
char *p;
…
if( (p!=0) && (strlen(p)>10) )
…
(2)
char *p;
…
if( (strlen(p)>10) && (p!=0) )
…
What’s the difference between these two if statements?
Hint: p is a null pointer.
Got your answer from the hint yet? The key is ‘&&’. According to its definition, ‘&&’ will stop evaluation as soon as left statement is a false expression. When p is a null pointer, (1)’s if statement will not perform strlen() statement since p!=0 is a false value. (2)’s if statement will perform strlen() statement since it is the first expression and it is going to be a BIG bomb.
Friday, August 22, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment