Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!caen!sol.ctr.columbia.edu!ira.uka.de!i30fs1!krey From: krey@i30fs1.NoSubdomain.NoDomain (Andreas Krey) Newsgroups: comp.lang.c Subject: Re: New 'n' Improved comp.lang.c FAQ List Message-ID: <1991Apr4.080448.23488@ira.uka.de> Date: 4 Apr 91 08:04:48 GMT References: <910401.0xf001@etiquette.uu.net> <1991Apr1.203024.19679@unlv.edu> <31946@shamash.cdc.com> <1991Apr4.024300.19969@garfield.cs.mun.ca> Sender: krey@i30fs1 (Andreas Krey) Organization: University of Karlsruhe, FRG Lines: 91 In article <1991Apr4.024300.19969@garfield.cs.mun.ca>, matthew1@garfield.cs.mun.ca (Matthew J. Newhook) writes: |> bls@u02.svl.cdc.com (Brian Scearce) writes: |> |> >grover@big-joe.cs.unlv.edu (Kevin Grover) writes: |> >> Apparently non-obvious April Fool's Day bogus account posts: |> >>> Q: Why doesn't this function work: |> >>> |> >>> itoa(int i) |> >>> { |> >>> char retbuf[5]; /* biggest int: 32769 */ |> >>> sprintf("%d", retbuf, i); |> >>> return retbuf; |> >>> } |> >>> |> |> >> A correct version of this program is: |> >> char *itoa(int i) |> >> { |> >> static char retbuf[5]; /* biggest int: 32769 */ |> >> sprintf(retbuf, "%d", i); |> >> return retbuf; |> >> } |> |> >Almost, but not quite, Mr. Grover. The *really* correct version of this: |> |> > char *itoa(int i) |> > { |> > static char retbuf[5]; /* biggest int: 32768 */ |> > sprintf(retbuf, "%d", i); |> > return retbuf; |> > } |> |> Actually here is a correct (portable) version (assuming that you want to |> keep a static character buffer, and not use a newly malloc'd one each time). |> |> #include |> |> char *itoa(int i) |> { |> static int first = 1; |> static char *s; |> |> if (first) { /* IF FIRST TIME ALLOCATE MEMORY FOR STRING */ |> first = 0; |> s = (char *) |> malloc((long) ceil(log10(pow(2, (double) sizeof(int) * 8))) +1); |> if (!s) { |> fprintf(stderr,"malloc failed\n"); |> exit(1); |> } |> } |> |> sprintf(s, "%d", i); |> return s; |> } |> |> This is portable, 'cause it always assigns the correct value to the size of |> the array, without depending on the sizeof(int) == 2. |> |> Matthew Newhook |> |> |> -- |> ----------------matthew1@garfield.cs.mun.ca |> "Living in the limelight; the universal dream for those who wish to |> seem. Those who wish to be must put aside the alienation, get on with |> the facination, the real relation, the underlying theme" - Rush Actually the correct simple solution is: char *itoa(int i) { static char retbuf[6]; /* biggest int: 32768 */ sprintf(retbuf, "%d", i); return retbuf; } since the resulting string contains five digits and a trailing zero. (note the [6]). If you do not rely on sizeof(int)==2, I usually set the array size to a sufficiently big value. 30 will probably be enough for the next few computer generations, and will then generate really nice (hard to find) bugs... (I didn't bother to check whether the complicated expression takes the trailing '\0' into account. Also I only read this group if there are fewer than articles, so my point may have been pointed out.) -- Andy ------------------------------------------- Zeit ist Geld. Aber Geld ist keine Zeit. [Intl: Time is money. But money isn't time.]