home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!pagesat!spssig.spss.com!uchinews!iitmax!elof.iit.edu!matujos
- From: matujos@elof.iit.edu (Joe Matusiewicz)
- Newsgroups: comp.lang.c
- Subject: Problem with string processing.
- Message-ID: <1993Jan2.233446.20833@iitmax.iit.edu>
- Date: 2 Jan 93 23:34:46 GMT
- Sender: news@iitmax.iit.edu (News)
- Organization: Illinois Institute of Technology, Chicago
- Lines: 35
-
-
- I am having a little problem with string processing. I have been
- programming in C for 6 months, and I just can't figure out the following
- problem:
-
- I need a function to append a character to the end of a string.
- I can't find an ANSI function to do so, so I tried to write my own.
- Here's what I have:
-
-
- void add_char_to_str(char ch, char *str)
- {
- 1. char *tmp;
- 2. tmp = (char *) calloc (2*sizeof(char));
- 3. *tmp = ch;
- 4. *(tmp+1) = '\0';
- 5. strcat(str, tmp);
- free (tmp);
- }
-
- What I tried to do here is to create a temporary string, tmp, which
- is 2 bytes long. The first byte is for the character, and the second is
- for the null character (to end the string). Then the temporary string
- is to be used with strcat to append it to the end of the target string.
-
- The problem seems to be in lines 3 and 4. The character, ch,
- doesn't appear as the first character in tmp after I stepped through
- it. I get "\\\ap" in my tmp string after the 4th line is executed.
-
- Can anyone point out what is wrong?
-
-
- Please E-mail me at: matujos@elof.iit.edu
-
- Joe.
-