NULL checks for nested pointers in one if statement
Struct {
int a;
struct sample *b;
} test;
int func(struct test *t1) {
if (!t1 || !t1->b) { // Is this statement ok?
return _EINVAL
}
...
}
Is it possible to check for NULL for nested pointers in a single if statement? Can I always assume that the first check (! T1 in my example) will be done first?
+3
source to share
1 answer
Yes, the C language rules ensure that if an expression a
is a || b
true, then it is b
never evaluated. (A similar rule exists for a && b
if a
is false.) This is called short circuit evaluation .
+7
source to share