Assumption

P

Peteris Krumins

Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)


P.Krumins
 
K

Kevin Goodsell

Peteris said:
Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

No assumption is necessary. The || and && operators are defined such
that they evaluate from left to right, stopping as early as possible. If
the right side is not needed in order to determine the result, it is not
evaluated.

-Kevin
 
T

Tom St Denis

Peteris Krumins said:
Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

Yes.

Double operators have left-to-right early-out as part of the standard.
Likewise

if (ptr_to_char != NULL && strlen(ptr_to_char) == 5) { ... }

Will only eval to true if it's not NULL and 5 chars in len, if it's NULL it
won't call strlen

Tom
 
K

Kelsey Bjarnason

Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

Correct; since only one case needs to be true, if the first is true, the
latter need not be evaluated at all.
 
C

CBFalconer

Peteris said:
Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

Yes. However evaluating the inverse condition is probably
cleaner, shorter, and clearer:

if (ptr_to_char && strlen(ptr_to_char)) {
/* go ahead */
}
else {
/* the code that originally followed your if */
}
 
E

Ed Morton

Peteris said:
Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

Yes, but remember that just because it isn't a NULL pointer doesn't mean
it points at a valid string as it may not have a terminating NUL
character in which case it still isn't safe to call strlen().

Ed.
 
P

Peter Nilsson

Peteris Krumins said:
Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

if (ptr_to_char == NULL || *ptr_to_char == 0)

But yes, the standard says...

The || operator shall yield 1 if either of its operands compare unequal
to 0; otherwise, it yields 0. The result has type int.

Unlike the bitwise | operator, the || operator guarantees left-to-right
evaluation; there is a sequence point after the evaluation of the first
operand. If the first operand compares unequal to 0, the second operand
is not evaluated.
 
M

Mantorok Redgormor

Peteris Krumins said:
Hello,

Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)


P.Krumins

If the first expression evaluates to true
short-circuiting takes effect and the rest
of the expression on the right side is ignored.

The first expression will always be evaluated first
because the logical operators && and ||
along with the comma and ternary operator
have left-to-right evaluation.
 
J

J. J. Farrell

Peteris Krumins said:
Can I assume that writing the following if/etc. statement,
the second part of it will not be evaluated at all if
ptr_to_char is/points_to NULL :

if (ptr_to_char == NULL || strlen(ptr_to_char) == 0)

It will not be evaluated if ptr_to_char is NULL.

It will be evaluated if ptr_to_char points to NULL
(whatever that means in this context).
 
P

pete

J. J. Farrell said:
It will not be evaluated if ptr_to_char is NULL.

It will be evaluated if ptr_to_char points to NULL
(whatever that means in this context).

I have no idea what that could mean in any context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top