home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15970 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  1.7 KB

  1. Path: sparky!uunet!news.centerline.com!matt
  2. From: matt@centerline.com (Matt Landau)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: const char *
  5. Date: 9 Nov 1992 15:23:41 GMT
  6. Organization: CenterLine Software, Inc.
  7. Lines: 48
  8. Message-ID: <1dlvptINNc3k@armory.centerline.com>
  9. References: <92311.184452KSADEGH@auvm.american.edu> <BxD5uz.90q@slipknot.rain.com>
  10. NNTP-Posting-Host: 140.239.1.32
  11.  
  12. In <BxD5uz.90q@slipknot.rain.com> robert@slipknot.rain.com (Robert Reed) writes:
  13. >In article <92311.184452KSADEGH@auvm.american.edu> Kayvon Z. Sadeghi <KSADEGH@auvm.american.edu> writes:
  14. >| mycall(const char *msg){
  15. >|     if(msg=="")
  16. >|          dothis();
  17. >|     if(msg!="")
  18. >|          dothat();
  19. >|}
  20. >|
  21.  
  22. >Your "" is not the same as your ""... Try something like:
  23. >
  24. >const char *Empty = "";
  25. >
  26. >mycall(const char *msg) {
  27. >    if (msg == Empty)
  28. >        dothis();
  29. >    else
  30. >        dothat();
  31.  
  32. The diagnosis is correct, but the solution is wrong.  This will have the 
  33. exact same effect as the original poster's code unless the calling site 
  34. declares "extern const char *Emtpy" and makes the call as 'mycall(Empty)' 
  35. instead of 'mycall("")'.  I don't think this is really what the original 
  36. poster had in mind.
  37.  
  38. Simply moving the string literal "" up and creating a global identifier
  39. whose value is "" won't change the fact that two pointers to char are 
  40. not "equal" simply because they point to different instances of the same
  41. character sequence.
  42.  
  43. This is likely to be closer to Kayvon's intent:
  44.  
  45.     void mycall(const char *msg)
  46.     {
  47.         if (msg == NULL || *msg == '\0')
  48.         dothis();
  49.         else
  50.         dothat();
  51.     }
  52.  
  53. with the additional feature that 'mycall(NULL)' now behaves the same as 
  54. 'mycall("")'.  For those who value brevity, the if condition can be written
  55. as 
  56.  
  57.         if (!msg || !*msg)
  58.  
  59. without loss of generality.
  60.