Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!uxc.cso.uiuc.edu!tank!shamash!com50!midgard!dal From: dal@midgard.Midgard.MN.ORG (Dale Schumacher) Newsgroups: comp.lang.c Subject: Re: In defense of scanf() (Re: Re^2: scanf(..)) Summary: Watch for '\0' matching with strchr() Message-ID: <1122@midgard.Midgard.MN.ORG> Date: 3 Aug 89 19:22:05 GMT References: <225800176@uxe.cso.uiuc.edu> <11831@bloom-beacon.MIT.EDU> <824@cbnewsl.ATT.COM> <4596@ficc.uu.net> Reply-To: dal@midgard.Midgard.MN.ORG (Dale Schumacher) Organization: The Midgard Realm, St Paul MN Lines: 24 In article <4596@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes: |In article <824@cbnewsl.ATT.COM>, mpl@cbnewsl.ATT.COM (michael.p.lindner) writes: |> Hope you never have to do anything complex. If you call strtok on e string |> in the middle of a strtok of another string it trashes its state information |> on the first string (a little known feature), which can cause extremely |> elusive bugs. | |Sounds like something too fancy for scanf, too. But thanks for the info... I've |never gotten that complex with strtok. By that time I'm usually stepping |through the string by hand (while (strchr(*s, legalchars)) s++;)... Be careful here. Since strchr() will match the '\0' at the end of legalchars, you may walk right out of the string if all characters are legal! I use a macro like the following: #define IN_SET(set,c) ((c) && strchr((set), (c))) Note, this is NOT a "safe" macro, so be sure c has no side-effects... |[files away information that there are broken implementations of strtok...] The operation of strtok() described above is NOT broken, it's documented. It is also somewhat less useful than it could be due to it's "interesting" quirks, but it IS defined as working that way.