1

I am trying to append three different const char* variables into one. This is because a function from windows library takes the parameter LPCTSTR. I have the following code:

const char* path = "C:\\Users\\xxx\\Desktop\\";
const char* archivo = "vectors";
const char* extension = ".txt";

const char* fullPath =+ path;
fullPath =+ archivo;
fullPath =+ extension;

When I run it I get only the last (extension) added to to FullPath.

Andy
  • 186
  • 1
  • 4
  • 23
  • Pointers cannot be appended to pointers. – juanchopanza Dec 17 '16 at 18:23
  • 5
    C-style strings don't support concatenation, and you do know what `const` means?. And what is `=+` supposed to be doing? Bottom line, use std::string. –  Dec 17 '16 at 18:24
  • 1
    `LPCTSTR` is not synonymous with `const char*`, by the way. This is a long pointer to a constant `TCHAR` array. `TCHAR` is transient, and can mean either `char` or `wchar_t` depending on how your software is setup. If you are using a Win32 function that takes an `LPCTSTR`, I would suggest you don't do that... the Win32 API has `A` and `W` variants for all string functions and any code that uses `char *` should be using the `A` variant. – Andon M. Coleman Dec 17 '16 at 18:29
  • i think if you just use `const wchar_t*` with wcscat all be cool – Алексей Неудачин Dec 17 '16 at 20:47

3 Answers3

9

You need to allocate some space to hold the concatenated strings. Fortunately, C++ has the std::string class to do this for you.

std::string fullPath = path;
fullPath += archivo;
fullPath += extension;
const char *foo = fullPath.c_str();

Be aware that the space containing the concatenated strings is owned by fullPath and the pointer foo will only remain valid so long as fullPath is in scope and unmodified after the call to c_str.

donjuedo
  • 2,475
  • 18
  • 28
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Since the OP didn't know about strings (I suppose), it might be valuable to add that the returned pointer is only valid while fullPath is in scope. – Shaggi Dec 17 '16 at 18:25
  • This doesn't address compile-time addition of `const char *` – Zach Jun 10 '22 at 18:08
3

If you want to construct at compile-time a bunch of string literals, some of which are concatenations of other string literals, the most basic idiomatic low-maintenance technique is based on the good-old preprocessor

#define PATH "C:\\Users\\xxx\\Desktop\\"
#define NAME "vectors"
#define EXT  ".txt"

const char *path = PATH;
const char *archivo = NAME;
const char *extension = EXT;

const char *fullPath = PATH NAME EXT;

However, the same thing can be achieved in more moden way by using some constexpr and template meta-programming magic (see C++ concat two `const char` string literals).

Otherwise, you will have to resort to run-time concatenation (like std::string and such). But again, if the input is known at compile time, then a run-time solution is a "loser's way out" :)

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

When you use a const char* you can't change the chars to which are pointing. So append const char* to a const char* is not possible!

Rama
  • 3,222
  • 2
  • 11
  • 26