Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!mintaka!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Subject: Re: help with strcat Message-ID: <1991Jun4.234648.5398@athena.mit.edu> Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Inc. References: <1991Jun4.210209.28463@ux1.cso.uiuc.edu> Distribution: comp Date: Tue, 4 Jun 91 23:46:48 GMT Lines: 26 In article <1991Jun4.210209.28463@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes: >cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes: >>could someone show me some example code to take >>two char strings and combine them into another char >>string. > >char *str1 = "Joseph went"; >char *str2 ' " to the store."; >char str3[100]; >strcat(str3, str1); /* tacks str1 onto the end of str3 */ >strcat(str3, str2); /* tacks str2 onto the end of str3 */ This may fail if str3 is a local variable, because it is not necessarily initialized with \0's. A safer technique would be (void)strcpy(str3, str1); (void)strcat(str3, str2); To the original requester: see also the comp.lang.c frequently- asked questions list, under the question "I can't get strcat to work." (The FAQ list also discusses static vs. automatic default initialization, although it does not explicitly mention \0 for char arrays.) Steve Summit scs@adam.mit.edu