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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!uchinews!iitmax!elof.iit.edu!matujos
  3. From: matujos@elof.iit.edu (Joe Matusiewicz)
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <1993Jan6.020420.25829@iitmax.iit.edu>
  6. Sender: news@iitmax.iit.edu (News)
  7. Organization: Illinois Institute of Technology, Chicago
  8. References: <1993Jan2.233446.20833@iitmax.iit.edu> <1993Jan4.191716.11483@eagercon.com>
  9. Date: Wed, 6 Jan 93 02:04:20 GMT
  10. Lines: 47
  11.  
  12.  
  13.     I'm the one that had the problem with string processing.  First,
  14. I'd like to thank all of you who mailed me back the answer to my problem.
  15. All the replys have been extremely helpful.  SeSecond, I just wanted to 
  16. post a followup article.  
  17.     The problem was most likely my use of calloc with one argument.  I
  18. first used malloc and forgot that calloc required 2 parameters, so I 
  19. accidentally forgot about that.   (But some of you might agree with me that
  20. the stupidest errors are usually the hardest to find).  Anyway, here is
  21. the final copy of my function (which is exactly what many of you suggested):
  22.  
  23. void add_char_to_str( char *str, int ch )
  24. {
  25.     while(*str != '\0')
  26.         str++;
  27.     *str++ = ch; 
  28.     *str = '\0';
  29. }
  30.  
  31. This funtion doesn't have to use dynamic memory allocation at all.  It 
  32. simply moves the pointer, str, (which is actually a copy of the pointer, thus
  33. it is not changed after the fuction is returned) to the end of the string, 
  34. and the character is simply copied to the end.  The NULL character is 
  35. appended after that to end the string to end it.  
  36.  
  37. I knew there had to be a simpler way, 'cause there always is.
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. P.S.- Thanks for all the additional tips cause I am in the process of learning
  45.       good C prgramming style.
  46.  
  47.  
  48.                                      Thanx again,
  49.                     Joe Matusiewicz.
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.