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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!cs.utexas.edu!uwm.edu!linac!unixhub!ditka!eagercon!eagercon!eager
  3. From: eager@eagercon.com (Michael J. Eager)
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <1993Jan4.191716.11483@eagercon.com>
  6. Sender: root@eagercon.com (Operator)
  7. Reply-To: eager@eagercon.com
  8. Organization: Eager Consulting
  9. References: <1993Jan2.233446.20833@iitmax.iit.edu>
  10. Date: Mon, 4 Jan 1993 19:17:16 GMT
  11. Lines: 40
  12.  
  13. In article 20833@iitmax.iit.edu, matujos@elof.iit.edu (Joe Matusiewicz) writes:
  14. >
  15. >
  16. >         void add_char_to_str(char ch, char *str)
  17. >         {
  18. >1.           char *tmp;
  19. >2.           tmp = (char *) calloc (2*sizeof(char));
  20.                              ^^^^^^^^^^^^^^^^^^^^^^^
  21. Calloc takes two parameters:  calloc (2,1)
  22. You should always include <stdlib.h> in your program when you use calloc; it
  23. would have allowed the compiler to flag the incompatibility.
  24.  
  25. >3.           *tmp = ch;
  26. >4.           *(tmp+1) = '\0';
  27. >5.           strcat(str, tmp);
  28. >             free (tmp);
  29. >         }
  30. >
  31.  
  32.  
  33. This is also a very awkward way to do a very simple task.  Here is another
  34. way:
  35.  
  36.  
  37. void add_char_to_str(char ch, char *str)
  38. {
  39.     while (*str++);        /* Skip to end of string */
  40.     *str++ = ch;        /* Add character */
  41.     *str = '\0';        /* Terminate string */
  42. }
  43.  
  44.  
  45. I'd also suggest that the argument ordering be reversed to make the function
  46. resemble strcat.
  47.  
  48. ---
  49. Michael J. Eager        Michael.Eager@eagercon.com
  50. Eager Consulting        (415) 325-8077 
  51. 1960 Park Boulevard, Palo Alto, CA 94306-1141
  52.  
  53.