File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes C / C++ and the fly likes char * comparision (&&) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "char * comparision (&&)" Watch "char * comparision (&&)" New topic

char * comparision (&&)

Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Please consider the following code snippet:

char buf[256];
//something here fills in buf correctly
char * cds = strstr(buf,"some string");

if (cds && cds == buf)
//does something

What I don't get in this code is what the && is trying to do there. What is the purpose of && in this example?

I see wind mills
Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
Only now have I noticed how simple my question was. Of course the && is the AND logical operator. What happens is that I have been dealing with bit operator (such as &) and was reading the expression of the IF statement as
(cds && cds) == true instead of cds && (cds == true), as if the && would do some operation between cds and itself before doing the comparison.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

The statement means "if cds is not the null pointer and cds is equal to buf". The whole first part isn't necessary. You could just as well write: if (cds == buf), because if cds == buf is true, then cds will never be the null pointer anyway.

Note that this only compares the pointers, it does not check if that what cds points to contains the same as what buf contains.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: char * comparision (&&)
jQuery in Action, 3rd edition