Mail Archives: djgpp/1999/08/12/22:56:45
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 -