home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!news.centerline.com!matt
- From: matt@centerline.com (Matt Landau)
- Newsgroups: comp.lang.c++
- Subject: Re: const char *
- Date: 9 Nov 1992 15:23:41 GMT
- Organization: CenterLine Software, Inc.
- Lines: 48
- Message-ID: <1dlvptINNc3k@armory.centerline.com>
- References: <92311.184452KSADEGH@auvm.american.edu> <BxD5uz.90q@slipknot.rain.com>
- NNTP-Posting-Host: 140.239.1.32
-
- In <BxD5uz.90q@slipknot.rain.com> robert@slipknot.rain.com (Robert Reed) writes:
- >In article <92311.184452KSADEGH@auvm.american.edu> Kayvon Z. Sadeghi <KSADEGH@auvm.american.edu> writes:
- >| mycall(const char *msg){
- >| if(msg=="")
- >| dothis();
- >| if(msg!="")
- >| dothat();
- >|}
- >|
-
- >Your "" is not the same as your ""... Try something like:
- >
- >const char *Empty = "";
- >
- >mycall(const char *msg) {
- > if (msg == Empty)
- > dothis();
- > else
- > dothat();
-
- The diagnosis is correct, but the solution is wrong. This will have the
- exact same effect as the original poster's code unless the calling site
- declares "extern const char *Emtpy" and makes the call as 'mycall(Empty)'
- instead of 'mycall("")'. I don't think this is really what the original
- poster had in mind.
-
- Simply moving the string literal "" up and creating a global identifier
- whose value is "" won't change the fact that two pointers to char are
- not "equal" simply because they point to different instances of the same
- character sequence.
-
- This is likely to be closer to Kayvon's intent:
-
- void mycall(const char *msg)
- {
- if (msg == NULL || *msg == '\0')
- dothis();
- else
- dothat();
- }
-
- with the additional feature that 'mycall(NULL)' now behaves the same as
- 'mycall("")'. For those who value brevity, the if condition can be written
- as
-
- if (!msg || !*msg)
-
- without loss of generality.
-