home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!friend!friend!rich
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: strtol() return values
- In-Reply-To: md@sco.COM's message of Tue, 26 Jan 1993 12:36:14 GMT
- Message-ID: <RICH.93Jan28094746@rich.kastle.com>
- Lines: 42
- Sender: news@friend@kastle.com (News)
- Organization: Kastle Development Associates, Arlington, VA, USA
- References: <ric.728002623@updike> <1993Jan26.123614.10840@sco.com>
- Date: Thu, 28 Jan 1993 09:47:46 GMT
-
- In article <1993Jan26.123614.10840@sco.com> md@sco.COM (Michael Davidson) writes:
-
- > ric@updike..sri.com (Richard Steinberger) writes:
- >
- > > strtol() returns a long int when successful. If a non-numeric
- > >string is encountered, 0 is returned. Unfortunately, errno doesn't seem
- > >to be set. Here's the question: If a string like, "0" or "000"
- > >is encountered, I also get a 0 returned from strtol(). Is there an easy,
- > >elegant way to check for this without examing each char?
- >
- > Yes - try something like this:
- >
- > char **p;
- >
- > if ((value = strtol(str, p, 0)) == 0 && *p == str)
- > printf("strtol() failed!!\n");
- >
-
- char *p;
- if ((value = strtol(str, &p, 0)) == 0 && p == str)
- printf("strtol() failed!!\n");
-
- Note the change from char **p to *p. The previous fragment left
- out (possibly as an exercise to the reader) the technicality of "where
- does p point?"
-
- Since failure is easily detected by the pointer compare, you can
- leave out the compare value==0:
-
- value = strtol(str, &p, 0);
- if (p == str)
- printf("strtol() failed!!\n");
-
- Unrelated question:
-
- I had to write a strtoul function because my environment didn't
- provide one. I couldn't find anything which says what strtoul should
- do if the base parameter is not 0 or 2 through 36. What should it do?
- Is this implementation-defined? (Mine just uses base ten.)
- --
- Richard Krehbiel rich@kastle.com or richk@grebyn.com
- OS/2 2.0 will do for me until AmigaDOS for the 386 comes along...
-