home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!noc.near.net!hri.com!spool.mu.edu!enterpoop.mit.edu!eru.mt.luth.se!lunic!sunic!aun.uninett.no!nuug!ifi.uio.no!nntp.ifi.uio.no!jar
- From: jar@solva.ifi.uio.no (Jo Are Rosland)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with string processing.
- Message-ID: <JAR.93Jan4201747@solva.ifi.uio.no>
- Date: 4 Jan 93 19:17:47 GMT
- References: <1993Jan2.233446.20833@iitmax.iit.edu> <1993Jan3.194154.1740@data-io.com>
- <1993Jan4.025351.26173@osuunx.ucc.okstate.edu>
- Sender: jar@ifi.uio.no (Jo Are Rosland)
- Organization: Dept. of Informatics, University of Oslo, Norway
- Lines: 61
- Nntp-Posting-Host: solva.ifi.uio.no
- In-Reply-To: gcouger@olesun.okstate.edu's message of 4 Jan 93 02:53:51 GMT
- X-Md4-Signature: fddf08a7e36cc2a09f8fb09a6d766c35
- Originator: jar@solva.ifi.uio.no
-
- In article <1993Jan4.025351.26173@osuunx.ucc.okstate.edu> Gordon Couger writes:
- > #include <strings.h>
- You don't need this, you don't use any string functions.
-
- > char *add_char(char *s, char c)
- You ought to use "int" as the type of "c".
-
- > {
- > char *sp = s;
- > while(*sp++)
- > ; /* finds end of string and leave pointer for adding char */
- > *sp-1 = c;
- This parse as "(*sp)-1 = c", where "(*sp)-1" isn't an lvalue. You
- probably meant "*(sp - 1) = c".
-
- > *sp = NULL;
- Don't use NULL, use '\0' or 0. NULL is a null pointer, not a character.
-
- > return s;
- I know this is the way the standard functions do it, but I've always
- wondered why. The end of the string is a more interesting value most
- of the time, as you can use that to add more to the end of the string
- without having to scan it from the beginning each time. If the
- functions strcpy() and strcat() did return the end of the string,
- they'd be a lot more efficient when building strings from many
- concatenations.
-
- > }
-
- > Gordon Couger
- > AB5Dg Agriculture Engineering Oklahoma State University
- > gcouger@olesun.agen.okstate.edu 405-744-6514 day 744-2794 evenings
-
- Try this one instead:
-
- char *add_char(char *s, int c)
- {
- char *sp = s;
- while (*sp++)
- ;
- *(sp - 1) = c;
- *sp = '\0';
- return s;
- }
-
- ...or this one, if you'd like to return the end of the string:
-
- char *add_char(char *s, int c)
- {
- while (*s++)
- ;
- *(s - 1) = c;
- *s = '\0';
- return s;
- }
-
- Hopefully my versions are also full of mistakes, so we can keep this
- thing going :-)
- --
- Jo Are Rosland
- jar@ifi.uio.no
-