delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/08/12/22:56:45

From: Moogla <ngoldber AT lan DOT tjhsst DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Catenation (spelled right?) of strings
Date: Thu, 12 Aug 1999 21:31:16 -0400
Organization: MindSpring Enterprises
Lines: 45
Message-ID: <37B37463.4CB8@lan.tjhsst.edu>
References: <37B36D0D DOT 7F00 AT lords DOT com>
NNTP-Posting-Host: a5.f7.49.35
Mime-Version: 1.0
X-Server-Date: 13 Aug 1999 01:37:22 GMT
X-Mailer: Mozilla 3.01 (Win95; I)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Robinson S. wrote:
> 
> Is it possible to group a bunch of strings into an array of char:
No, unfortunately not.

> 
> I tried this:
> char THE_STRING [255];
> THE_STRING = "GOATS " + "MAKE " + "GOOD " + "PETS!";
> 
> My compiler (DGJPP gccw32.exe) says: "invalid operands to binary +"
Exactly, + cannot add string constants (since addition is always the
mathmathical kind)

> 
> How do I join those strings?

1) use the C library function strcat(char *, char *)

char * THE_STRING = strcat("GOATS", "MAKE);
char * THE_STRING = strcat(THE_STRING, "GOOD");
etc...

2) Write code to do it yourself if there is a case where you feel you
could do it better than strcat used multiple times.

To actually do a strcat yourself, you need:

char * some_base_string //The string to add on to...
char * some_other_string //The string we want to concatenate

Find the first character in some_base_string that is 0
This is because C strings are "NULL terminated") example:
char * temp = some_base_string;
for(; *temp; temp++); 		

Then start copying from some_other_string to some_base_string.
Once you get the the end of some_other_string, make sure to copy
the final NULL to some_base_string so it has an end marker too.

for(char * temp2 = some_other_string; *temp2; temp++, temp2++)
   *temp = *temp2;
*temp = 0;

moogla

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019