home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19197 < prev    next >
Encoding:
Text File  |  1993-01-04  |  2.4 KB  |  78 lines

  1. 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
  2. From: jar@solva.ifi.uio.no (Jo Are Rosland)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <JAR.93Jan4201747@solva.ifi.uio.no>
  6. Date: 4 Jan 93 19:17:47 GMT
  7. References: <1993Jan2.233446.20833@iitmax.iit.edu> <1993Jan3.194154.1740@data-io.com>
  8.     <1993Jan4.025351.26173@osuunx.ucc.okstate.edu>
  9. Sender: jar@ifi.uio.no (Jo Are Rosland)
  10. Organization: Dept. of Informatics, University of Oslo, Norway
  11. Lines: 61
  12. Nntp-Posting-Host: solva.ifi.uio.no
  13. In-Reply-To: gcouger@olesun.okstate.edu's message of 4 Jan 93 02:53:51 GMT
  14. X-Md4-Signature: fddf08a7e36cc2a09f8fb09a6d766c35
  15. Originator: jar@solva.ifi.uio.no
  16.  
  17. In article <1993Jan4.025351.26173@osuunx.ucc.okstate.edu> Gordon Couger writes:
  18. >   #include <strings.h>
  19. You don't need this, you don't use any string functions.
  20.  
  21. >   char *add_char(char *s, char c)
  22. You ought to use "int" as the type of "c".
  23.  
  24. >      {
  25. >      char *sp = s;
  26. >      while(*sp++)
  27. >     ;  /* finds end of string and leave pointer for adding char */
  28. >      *sp-1 = c; 
  29. This parse as "(*sp)-1 = c", where "(*sp)-1" isn't an lvalue.  You
  30. probably meant "*(sp - 1) = c".
  31.  
  32. >      *sp = NULL;
  33. Don't use NULL, use '\0' or 0.  NULL is a null pointer, not a character.
  34.  
  35. >      return s;
  36. I know this is the way the standard functions do it, but I've always
  37. wondered why.  The end of the string is a more interesting value most
  38. of the time, as you can use that to add more to the end of the string
  39. without having to scan it from the beginning each time.  If the
  40. functions strcpy() and strcat() did return the end of the string,
  41. they'd be a lot more efficient when building strings from many
  42. concatenations.
  43.  
  44. >      }
  45.  
  46. >   Gordon Couger
  47. >   AB5Dg   Agriculture Engineering Oklahoma State University
  48. >   gcouger@olesun.agen.okstate.edu 405-744-6514 day 744-2794 evenings   
  49.  
  50. Try this one instead:
  51.  
  52.     char *add_char(char *s, int c)
  53.     {
  54.         char *sp = s;
  55.         while (*sp++)
  56.             ;
  57.         *(sp - 1) = c;
  58.         *sp = '\0';
  59.         return s;
  60.     }
  61.  
  62. ...or this one, if you'd like to return the end of the string:
  63.  
  64.     char *add_char(char *s, int c)
  65.     {
  66.         while (*s++)
  67.             ;
  68.         *(s - 1) = c;
  69.         *s = '\0';
  70.         return s;
  71.     }
  72.  
  73. Hopefully my versions are also full of mistakes, so we can keep this
  74. thing going :-)
  75. --
  76. Jo Are Rosland
  77. jar@ifi.uio.no
  78.