Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize already declared char array in C++

Tags:

c++

I want to use something like this:

char theArray[]=new char[8];
theArray= { 1,2,3,4,5,6,7,8};

instead of

char theArray[] = { 1,2,3,4,5,6,7,8};

Is a similar thing possible?

like image 659
Charlie Brown Avatar asked Nov 30 '22 09:11

Charlie Brown


1 Answers

C++0x

char* ch;
ch = new char[8]{1, 2, 3, 4, 5, 6, 7, 8};

@David Thornley: then switch these lines and there is no problem. And seriously you're talking about reallocating char[8] in the same memory pool as the previous value, then you need to play with own allocators, something like:

char* ch1 = new char[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i'};
char* ch2 = new(ch1) char[8]{1, 2, 3, 4, 5, 6, 7, 8};

afaik OP is not likely to need that.

like image 126
erjot Avatar answered Dec 08 '22 00:12

erjot