Uniform initialization | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Uniform initialization

Starting from C++11 one can use uniform initialization syntax with array new-expressions: char* arr1 = new char[4]{'a','b','c' }; char* arr2 = new char[4]{ "abc" }; But how to initialize the string that is contained in the array (with uniform initialization syntax)? char* arr3 = new char[4]{ arr1 }; //Error: a value of type "char*" cannot used to initialize an entity of type "char"

3rd Jan 2019, 11:23 AM
JanSeliv - avatar
1 Answer
+ 2
The problem here is that new cannot be used to assign arrays given as arguments. Arrays must be given directly to new with {} enclosing the content (Known as a brace-enclosed initializer list, used for direct initialization). More details are given here: https://stackoverflow.com/a/27663805 Even arr2's construction throws an error as the const char* literal acts as an argument rather than a brace-enclosed initializer list.
3rd Jan 2019, 11:43 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar