aspose file tools*
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
Author

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.

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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
 
wood burning stoves
 
subject: char * comparision (&&)