File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char * comparision (&&)

 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rodrigo Bossini
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic