1

This code is not compilable:

LPSTR a1 = "a1";
LPSTR lpResult = a1 + "a2";

How can I get a long pointer lpResult to the "a1a2" string?

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
NieAR
  • 1,385
  • 6
  • 20
  • 31
  • 7
    It would behoove you to obtain [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis May 01 '12 at 17:26
  • 1
    ... the point being that character array strings don't work like that. `std::string` does though. Here, you'd need to allocate new memory to store the total string then copy both parts into it to make that work. And the "long pointer" part is historic: there's no distinction between long pointers and not on Windows any more. – Rup May 01 '12 at 17:27
  • 2
    Many languages allow you to concatenate strings like that with the + operator, but not C. as @Rup mentions though, you could do that with `std::string` – Eric Petroelje May 01 '12 at 17:28

1 Answers1

2

One option is to use the std::string concatenation. You can also use Microsoft's StringCchCat function.

Here's an example:

#include <Strsafe.h>

//... later in your code:

LPSTR a1 = "a1";
LPSTR a2 = "a2";

TCHAR dest[ 5 ];
StringCchCopy(dest, 5, a1);  // copy "a1" into the buffer
StringCchCat(dest, 5, a2);   // concatenate "a2" into the buffer
craigmj
  • 4,827
  • 2
  • 18
  • 22