Initialization of elements in dynamic array

I'm not entirely sure this type of question should go in the beginner forum, but I'll try my luck here first.

My question is; Is there any way to initialize all the values of the elements in a dynamic array a more efficient way than:
1
2
3
4
5
6
char * symbLi = new char[5];
symbLi[0] = '?';
symbLi[1] = '!';
symbLi[2] = '(';
symbLi[3] = ')';
symbLi[4] = '@';


I was imagining something like the standard array initialization.
 
char symbLi[5] = { '?', '!', '(', ')', '@' };


It's kind of annoying having to type the pointer name every time.
Your way is efficient. Just not pretty. I'd just use std::array.

c++17: http://en.cppreference.com/w/cpp/experimental/make_array
c++11: std::array<char, 5> arr = { '?', '!', '(', ')', '@' };
Last edited on
These are all possible:
1
2
3
4
5
char* symbLi = new char[5] { '?', '!', '(', ')',  '@' } ;
char* symbLi2 = new char[10] { '?', '!', '(', ')',  '@' } ; // rest are zero initialised
std::unique_ptr< char[] > symbLi3 { new char[5] { '?', '!', '(', ')',  '@' } } ; //managed by the unique pointer
std::vector<char> symbLi4 { '?', '!', '(', ')',  '@' } ;
std::string symbLi5 = "?!()@" ;
Topic archived. No new replies allowed.