home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15900 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.1 KB  |  41 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ukma!darwin.sura.net!uvaarpa!murdoch!virginia.edu!gs4t
  3. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  4. Subject: Re: const char *
  5. Message-ID: <1992Nov7.014947.878@murdoch.acc.Virginia.EDU>
  6. Sender: usenet@murdoch.acc.Virginia.EDU
  7. Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  8. Organization: University of Virginia
  9. References:  <92311.184452KSADEGH@auvm.american.edu>
  10. Date: Sat, 7 Nov 1992 01:49:47 GMT
  11. Lines: 28
  12.  
  13. Kayvon Z. Sadeghi <KSADEGH@auvm.american.edu> writes:
  14. : mycall("");
  15. : and then somewhere down in my code I have:
  16. :  mycall(const char *msg){
  17. :      if(msg=="")
  18. :           dothis();
  19. :      if(msg!="")
  20. :           dothat();
  21. : }
  22. : Strange, but my compiler doesthat() instead of dothis().
  23. : I even checked the value of msg and it *IS* equal to "". Is something wrong
  24. : with my C++ compiler or what? or are strings treated differently in here?
  25.  
  26. You are comparing the addresses of msg and the string "".
  27. Instead, compare the string values as follows:
  28.  
  29. void mycall (const char* msg) {
  30.     if (::strcmp(msg, "") == 0) // (msg && *msg==0) would be better here
  31.         dothis();
  32.     else
  33.         dothat();
  34. }
  35.  
  36. -Sekar
  37.