Possible Duplicate:
C String Concatenation
have the following code:
char *doencode(const unsigned char *input, int length)
{
//irrelevant code
}
char *fname, *lname;
char *encoded, *name;
name = fname + "|" + lname;
encoded = doencode(name, 30);
and I get this error: invalid operands to binary +
How can I combine fname & | & lname?
You can't simply add strings together in C because strings are actually just pointers to character arrays. What you have to do is allocate storage for a new string and copy the two strings per character into it.
You cannot concatenate
char*
andchar[]
(the"|"
) or any permutation of using+
. Usestrncat()
orsnprintf()
instead and ensure the destination buffer has enough memory to store the final string.the C is not like java script. This is not correct in c:
you can do it in this way:
The
name
pointer should be pointed to sufficient memory space.+2: +1 for
"|"
and +1 fornull
at the end of the stringExample