473,405 Members | 2,261 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,405 software developers and data experts.

Equal to operand

Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}
Nov 14 '05 #1
8 1275
rasayani wrote:
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}


This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q17.4.html

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #2
rasayani <vi****@hotmail.com> scribbled the following:
Does anyone know what the advantage is of using the following statement char *p = NULL;
if (NULL == p)
{ } over char *p = NULL;
if (p == NULL)
{ }


The advantage is that if you mistakenly type = instead of ==, the first
form will cause a constraint violation or a syntax error, while the
second form will silently compile and cause p to always be NULL.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You could take his life and..."
- Mirja Tolsa
Nov 14 '05 #3
vi****@hotmail.com (rasayani) wrote:
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}


Yes. It will confuse newbies. It is also said to have a beneficiary
effect on typos, but I suspect the placebo effect.

Richard
Nov 14 '05 #4
In article <28**************************@posting.google.com >,
vi****@hotmail.com (rasayani) wrote:
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}


Some people use the first form because in that case typing "=" instead
of "==" by accident will produce a syntax error that the compiler will
catch.

On the other hand, turn all warnings on on your compiler (I certainly
hope you do that), and it is most likely that you will get a warning for

if (p = NULL)

as well. And the first version is less readable, which will cost you
long term in maintenance cost.
Nov 14 '05 #5
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40*****************@news.individual.net>...
vi****@hotmail.com (rasayani) wrote:
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}


Yes. It will confuse newbies. It is also said to have a beneficiary
effect on typos, but I suspect the placebo effect.

Richard

exactly
Nov 14 '05 #6
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
The advantage is that if you mistakenly type = instead of ==, the first
form will cause a constraint violation or a syntax error, while the
second form will silently compile and cause p to always be NULL.


Although some helpful compilers may warn you about a possibly
incorrect assignment. Such warnings are a double-edged sword, because
you get reprimanded whether you actually made a mistake or not.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #7

"rasayani" <vi****@hotmail.com> wrote in message
news:28**************************@posting.google.c om...
Does anyone know what the advantage is of using the following statement

char *p = NULL;
if (NULL == p)
{

}

over

char *p = NULL;
if (p == NULL)
{

}


If you mistakenly type

if (p = NULL) {...}

then p will be assigned NULL instead of compared against null, and the
branch will never be taken, which is a bug. On the other hand, if you
mistakenly type

if (NULL = p) {...}

you will raise a diagnostic at compile time, since NULL cannot be assigned
to. So the NULL == p form is a good way to catch a certain class of typo,
but only in cases where one of the operands is not an lvalue.

For this *specific* instance, you can avoid the headache completely by
typing

if (!p) {...}

since !p is equivalent to p == NULL.

It would have been nice if dmr had realized early on that allowing
assignment expressions in conditionals could lead to problems, or if he had
used a different operator for assignment or equality (i.e., := and ==, or =
and :=, or something where the two operators don't start with the same
character). But he didn't, and we're stuck with the consequences.
Nov 14 '05 #8
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
[redacted]
Some people use the first form because in that case typing "=" instead
of "==" by accident will produce a syntax error that the compiler will
catch.


Also because people take MS samples as gospel.
Nov 14 '05 #9

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

Similar topics

6
by: DJ Craig | last post by:
I keep getting this error: Fatal error: Unsupported operand types in /usr/local/etc/httpd/htdocs/djtricities/kat/bpmchart/chart.php on line 58 I've looked for hours for some type of error, and...
2
by: Tim Mierzejewski | last post by:
You guys were so great with answering my first question that I've decided to ask you yet again! Again, only the relevant code is included. typedef unsigned int unint; #include <iostream> ...
2
by: Kostatus | last post by:
The following code worked perfectly fine until i modified an unrelated part of the program: this->dungArea.mBlocked = mioS.mio_dungArea.mFlags & isBlocked; after that i started getting the...
36
by: Vivek Mandava | last post by:
Hello All, Which one is an expensive operation? '>' or '==" ? I know this won't matter much. However, if I'm executing this operation million times, I would prefer the better choice. My...
15
by: Patrick.O.Ige | last post by:
Hi All i'm getting error with my TreeView Menu(I want to have a button with ExpandALL and CollapseALL):- Error:- 'AddressOf' operand must be the name of a method; no parentheses are needed. I...
1
by: programmingChick | last post by:
I have a program where I want to accept an operand (from a file) and apply that operand to some integers. How should I do that? Is the best way to convert it to its ascii character then figure out...
19
by: Rajesh S R | last post by:
Consider the following code: int main() { struct name { long a; int b; long c; }s={3,4,5},*p; p=&s;
1
by: siresoth666 | last post by:
Greeting to everyone, I am a rookie at ASP and this may seem like a very simpleton question to most but I am clueless about it. I copied a code from a tutorial on to Flash its ASP but it does not...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
11
by: Bernard.Mangay | last post by:
The remainder is non zero due to rounding errors. How can I remove the rounding errors?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.