Mail Archives: djgpp/1999/08/12/22:28:45
"Robinson S." wrote:
>
> Is it possible to group a bunch of strings into an array of char:
>
> I tried this:
> char THE_STRING [255];
> THE_STRING = "GOATS " + "MAKE " + "GOOD " + "PETS!";
>
> My compiler (DGJPP gccw32.exe) says: "invalid operands to binary +"
>
> How do I join those strings?
The correct thing to do is:
char *theString = "Goats " "make " "good " "pets!";
Your code has two problems:
1. The plus ('+') operator cannot be used on C-style strings. The
concatenate two literal strings, you simply put them side by side. Not
that this worked only for literal strings, and wouldn't work for
variables.
2. char THE_STRING[255]; declares THE_STRING as an array. Once an
array is declared, you cannot change it. The line "THE_STRING = ..." is
seen by the compiler as an attempt to change THE_STRING, and will not
compile. Other attempts to change THE_STRING itself like THE_STRING++
will also cause a compile time error. The individual elements of the
array can be changed, though, e.g., THE_STRING[0] = 'G'; THE_STRING[1] =
'O'; are legal.
--
Weiqi Gao
weiqigao AT a DOT crl DOT com
- Raw text -