0

I'm doing something like that:

char* test = "HELLO";
char* test2[6] = test; //

But it is not working, how can I achieve that?

Kirill
  • 105
  • 2
  • 8

2 Answers2

1

You can't copy arrays in C++, at least not without a little help. In this case the function you need is strcpy

char* test = "HELLO";
char test2[6];
strcpy(test2, test);

Also note that an array of chars is char[] not char*[] (which is an array of char pointers).

john
  • 85,011
  • 4
  • 57
  • 81
0

You can only initialize an array with a string literal:

8.5.2 Character arrays [dcl.init.string]
1 An array of narrow character type (3.9.1), char16_t array, char32_t array, or wchar_t array can be initialized by a narrow string literal, char16_t string literal, char32_t string literal, or wide string literal, respectively, or by an appropriately-typed string literal enclosed in braces (2.13.5). Successive characters of the value of the string literal initialize the elements of the array. [ Example:

char msg[] = "Syntax error on line %s\n";

I don't know the rationale for this but I'll take a guess and say that that it is meant to guarantee that direct initializations do not overflow the array (when the size is specified).

imreal
  • 10,178
  • 2
  • 32
  • 48