help with strcat

Steve Summit scs at adam.mit.edu
Wed Jun 5 09:46:48 AEST 1991


In article <1991Jun4.210209.28463 at ux1.cso.uiuc.edu> gordon at osiris.cso.uiuc.edu (John Gordon) writes:
>cshort at 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 at adam.mit.edu



More information about the Comp.lang.c mailing list