home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19141 < prev    next >
Encoding:
Internet Message Format  |  1993-01-02  |  1.5 KB

  1. Path: sparky!uunet!olivea!pagesat!spssig.spss.com!uchinews!iitmax!elof.iit.edu!matujos
  2. From: matujos@elof.iit.edu (Joe Matusiewicz)
  3. Newsgroups: comp.lang.c
  4. Subject: Problem with string processing.
  5. Message-ID: <1993Jan2.233446.20833@iitmax.iit.edu>
  6. Date: 2 Jan 93 23:34:46 GMT
  7. Sender: news@iitmax.iit.edu (News)
  8. Organization: Illinois Institute of Technology, Chicago
  9. Lines: 35
  10.  
  11.  
  12.    I am having a little problem with string processing.  I have been
  13. programming in C for 6 months, and I just can't figure out the following
  14. problem:
  15.  
  16.    I need a function to append a character to the end of a string.
  17. I can't find an ANSI function to do so, so I tried to write my own.
  18. Here's what I have:
  19.  
  20.  
  21.          void add_char_to_str(char ch, char *str)
  22.          {
  23. 1.           char *tmp;
  24. 2.           tmp = (char *) calloc (2*sizeof(char));
  25. 3.           *tmp = ch;
  26. 4.           *(tmp+1) = '\0';
  27. 5.           strcat(str, tmp);
  28.              free (tmp);
  29.          }
  30.  
  31.    What I tried to do here is to create a temporary string, tmp, which
  32. is 2 bytes long.  The first byte is for the character, and the second is
  33. for the null character (to end the string).  Then the temporary string
  34. is to be used with strcat to append it to the end of the target string.
  35.  
  36.    The problem seems to be in lines 3 and 4.  The character, ch,
  37. doesn't appear as the first character in tmp after I stepped through
  38. it.  I get "\\\ap" in my tmp string after the 4th line is executed.
  39.  
  40. Can anyone point out what is wrong?
  41.  
  42.  
  43.             Please E-mail me at:   matujos@elof.iit.edu
  44.  
  45.                                    Joe.
  46.