473,408 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Assumption

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
Nov 14 '05 #1
9 1265
Peteris Krumins wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2

"Peteris Krumins" <pk****************@inbox.lv> wrote in message
news:Xn*******************************@130.133.1.4 ...
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
Nov 14 '05 #3
On Sat, 10 Jan 2004 00:54:45 +0000, Peteris Krumins wrote:
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.
Nov 14 '05 #4
Peteris Krumins wrote:

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 */
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5


Peteris Krumins wrote:
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.

Nov 14 '05 #6
"Peteris Krumins" <pk****************@inbox.lv> wrote in message
news:Xn*******************************@130.133.1.4 ...
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.

--
Peter
Nov 14 '05 #7
Peteris Krumins <pk****************@inbox.lv> wrote in message news:<Xn*******************************@130.133.1. 4>...
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.

--
nethlek
Nov 14 '05 #8

"Peteris Krumins" <pk****************@inbox.lv> wrote in message
news:Xn*******************************@130.133.1.4 ...

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).
Nov 14 '05 #9
J. J. Farrell wrote:

"Peteris Krumins" <pk****************@inbox.lv> wrote in message
news:Xn*******************************@130.133.1.4 ...

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).


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

--
pete
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
1
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
1
by: Tony Johansson | last post by:
Hello Experts! I'm reading about design patter in the GAMMA book and there is something that I don't understand. That's why I ask you. It says "Pluggable adpters. A class is more reusable when...
2
by: Saintor | last post by:
I have a string of email addresses with duplicates. like "abc@abc.com; cde@cde.com; abc@abc.com" If I use this string with sendobject, the receiver will get it only once which is perfect to...
3
by: Peter Fox | last post by:
I have no intention of using any such mechanism to distinguish users of my PHP applications, but I was wondering (and it isn't easy for me to test) ... ....On a single PC with a single browser...
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, would it be safe to assume that the majority of the browsers out there can handle iframes? thanks, rodchar
5
by: Joza | last post by:
Hi! I'm getting error when selecting items in ListView control, it says ArguementOutOfRangeExceptions. That happens in SelectIndexChange event. I have tryed with try...catch but there's no...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.