home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!usc!cs.utexas.edu!uwm.edu!linac!unixhub!ditka!eagercon!eagercon!eager
- From: eager@eagercon.com (Michael J. Eager)
- Subject: Re: Problem with string processing.
- Message-ID: <1993Jan4.191716.11483@eagercon.com>
- Sender: root@eagercon.com (Operator)
- Reply-To: eager@eagercon.com
- Organization: Eager Consulting
- References: <1993Jan2.233446.20833@iitmax.iit.edu>
- Date: Mon, 4 Jan 1993 19:17:16 GMT
- Lines: 40
-
- In article 20833@iitmax.iit.edu, matujos@elof.iit.edu (Joe Matusiewicz) writes:
- >
- >
- > void add_char_to_str(char ch, char *str)
- > {
- >1. char *tmp;
- >2. tmp = (char *) calloc (2*sizeof(char));
- ^^^^^^^^^^^^^^^^^^^^^^^
- Calloc takes two parameters: calloc (2,1)
- You should always include <stdlib.h> in your program when you use calloc; it
- would have allowed the compiler to flag the incompatibility.
-
- >3. *tmp = ch;
- >4. *(tmp+1) = '\0';
- >5. strcat(str, tmp);
- > free (tmp);
- > }
- >
-
-
- This is also a very awkward way to do a very simple task. Here is another
- way:
-
-
- void add_char_to_str(char ch, char *str)
- {
- while (*str++); /* Skip to end of string */
- *str++ = ch; /* Add character */
- *str = '\0'; /* Terminate string */
- }
-
-
- I'd also suggest that the argument ordering be reversed to make the function
- resemble strcat.
-
- ---
- Michael J. Eager Michael.Eager@eagercon.com
- Eager Consulting (415) 325-8077
- 1960 Park Boulevard, Palo Alto, CA 94306-1141
-
-