Initialization of array on heap

Posted by Radek Šimko on Stack Overflow See other posts from Stack Overflow or by Radek Šimko
Published on 2010-12-31T16:30:32Z Indexed on 2010/12/31 17:54 UTC
Read the original article Hit count: 174

Filed under:
|
|
|

How do i manually initiate values in array on heap? If the array is local variable (in stack), it can be done very elegant and easy way, like this:

int myArray[3] = {1,2,3};

Unfortunately, following code

int * myArray = new int[3];
myArray = {1,2,3};

outputs an error by compiling

error: expected primary-expression before ‘{’ token
error: expected `;' before ‘{’ token

Do i have to use cycle, or not-so-much-elegant way like this?

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

© Stack Overflow or respective owner

Related posts about c++

Related posts about stack