home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!math.fu-berlin.de!news.th-darmstadt.de!rbg.informatik.th-darmstadt.de!misar
- From: misar@rbg.informatik.th-darmstadt.de (walter misar)
- Subject: Re: adding strings together
- Sender: news@news.th-darmstadt.de (The News System)
- Message-ID: <1993Jan4.121051@rbg.informatik.th-darmstadt.de>
- Date: Mon, 4 Jan 1993 11:10:51 GMT
- References: <83017@mavenry.UUCP>
- Nntp-Posting-Host: rbhp87.rbg.informatik.th-darmstadt.de
- Organization: TH Darmstadt
- Lines: 31
-
- In article <83017@mavenry.UUCP>, nider@mavenry.UUCP writes:
- > can't you just add strings together with a +, like:
- >
- > string3=string1+string2
-
- Use LPC and it will work (unfortunately LPC is only for MUDS :)
- In C++ you could overload + on char *, I think.
- But in ordinary C you have to use strcat (do an #include <string.h>).
-
- It will look like this (ANSI version):
-
- #include <string.h>
- #include <stdlib.h> /* for malloc */
-
- char string1[]="foo", string2[]="bar", *string3;
-
- int main(int argc, char *argv[])
- { string3=malloc(strlen(string1)+strlen(string2)+1); /* provide storage */
- if (!string3) out_of_memory(); /* some error action */
- strcpy(string3,string1); /* copy string1 to string 3 */
- strcat(string3,string2); /* add string2 to string3 */
-
- puts(string3); /* or anything else you want to do with string3 */
-
- ....
- return 0;
- }
-
- --
- Walter Misar It is impossible to enjoy idling thoroughly
- misar@rbg.informatik.th-darmstadt.de unless one has plenty of work to do.
-