How do I add two strings?
I tried name = "derp" + "herp";, but I got an error:
Expression must have integral or enum type
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
C does not have the support for strings that some other languages have. A string in C is just a pointer to an array of
charthat is terminated by the first null character. There is no string concatenation operator in C.Use
strcatto concatenate two strings. You could use the following function to do it:This is not the fastest way to do this, but you shouldn’t be worrying about that now. Note that the function returns a block of heap allocated memory to the caller and passes on ownership of that memory. It is the responsibility of the caller to
freethe memory when it is no longer needed.Call the function like this:
If you did happen to be bothered by performance then you would want to avoid repeatedly scanning the input buffers looking for the null-terminator.
If you are planning to do a lot of work with strings then you may be better off using a different language that has first class support for strings.