views:

96

answers:

4

Hello,

i would like to know if this looks correct :

while((next !=NULL) && (strcmp(next->name, some_string) < 0) {
    //some process
}

i mean, if next is NULL, then the second part of the expression won't be ever tested by the compiler ? i have heard that in C++ it's the case (but i'm not even sure of it).

Can someone confirm me that i won't get strange errors on some compilers with that ?

+4  A: 

It's definitely the case in both C and C++.

Jackson Pope
Correction: It's definitely true in C, so it should be true in C++.
ruslik
@ruslik - that's no correction. C++ and C are covered by standards, and this answer is correct about what both standards say (for built-in types in C++)
Daniel Earwicker
+6  A: 

Yes, in C++ short circuit and and or operators are available.

Here's a question answered in the C-faq on the subject.

Pablo Santa Cruz
A: 

This will work with lazy evaluation (the second statement not evaluated if the first one is evaluated to "false") unless your compiler is so non-standard compliant it can't even be named a C compiler. Millions lines of code in the field rely on this behavior, so you can think that this behavior is just guaranted.

sharptooth
+1  A: 

Yes && is short circuited and you are using it correctly.
If next is NULL string compare will never happen.

codaddict