Use new operator to initialise an array

后端 未结 2 1747
一生所求
一生所求 2021-02-02 08:34

I want to initialise an array in the format that uses commas to separate the elements surrounded in curly braces e.g:

int array[10]={1,2,3,4,5,6,7,8,9,10};
         


        
2条回答
  •  时光说笑
    2021-02-02 09:16

    You can use memcpy after the allocation.

    int originalArray[] ={1,2,3,4,5,6,7,8,9,10};
    int *array = new int[10];
    memcpy(array, originalArray, 10*sizeof(int) );
    

    I'm not aware of any syntax that lets you do this automagically.

    Much later edit:

    const int *array = new int[10]{1,2,3,4,5,6,7,8,9,10};
    

提交回复
热议问题