home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19175 < prev    next >
Encoding:
Text File  |  1993-01-04  |  1.5 KB  |  44 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!math.fu-berlin.de!news.th-darmstadt.de!rbg.informatik.th-darmstadt.de!misar
  3. From: misar@rbg.informatik.th-darmstadt.de (walter misar)
  4. Subject: Re: adding strings together
  5. Sender: news@news.th-darmstadt.de (The News System)
  6. Message-ID: <1993Jan4.121051@rbg.informatik.th-darmstadt.de>
  7. Date: Mon, 4 Jan 1993 11:10:51 GMT
  8. References:  <83017@mavenry.UUCP>
  9. Nntp-Posting-Host: rbhp87.rbg.informatik.th-darmstadt.de
  10. Organization: TH Darmstadt
  11. Lines: 31
  12.  
  13. In article <83017@mavenry.UUCP>, nider@mavenry.UUCP writes:
  14. >  can't you just add strings together with a +, like:
  15. >  
  16. >  string3=string1+string2
  17.  
  18. Use LPC and it will work (unfortunately LPC is only for MUDS :)  
  19. In C++ you could overload + on char *, I think.
  20. But in ordinary C you have to use strcat (do an #include <string.h>).
  21.  
  22. It will look like this (ANSI version):
  23.  
  24. #include <string.h>
  25. #include <stdlib.h>    /* for malloc    */
  26.  
  27. char string1[]="foo", string2[]="bar", *string3;
  28.  
  29. int main(int argc, char *argv[])
  30. { string3=malloc(strlen(string1)+strlen(string2)+1);    /* provide storage */
  31.   if (!string3) out_of_memory(); /* some error action    */
  32.   strcpy(string3,string1);     /* copy string1 to string 3    */
  33.   strcat(string3,string2);    /* add string2 to string3    */
  34.  
  35.   puts(string3);    /* or anything else you want to do with string3    */
  36.  
  37. ....
  38.   return 0;
  39. }
  40.  
  41. -- 
  42. Walter Misar                        It is impossible to enjoy idling thoroughly
  43. misar@rbg.informatik.th-darmstadt.de       unless one has plenty of work to do.
  44.