c++ - How Do I Use a Variable in new[]'s Value Initialization -


so when newing array of chars can value initialize:

const char* foo = new char[4]{'j', 'o', 'n', '\0'}; 

what want know how use variable in initializer_list:

const string initializer{"jon"}; const char* foo = new char[4]{initializer.c_str()}; // doesn't work, can make work? 

you can use variable, can't use string , expect compiler magically split up.

const char* ip = initializer.c_str(); const char* foo = new char[4]{ip[0], ip[1], ip[2], ip[3]}; 

once have variable length, though, doesn't work @ all. have allocate , use string::copy fill array in separate step.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -