c++ initial value of dynamic array

后端 未结 6 712
清酒与你
清酒与你 2020-12-15 17:12

I need to dynamically create an array of integer. I\'ve found that when using a static array the syntax

int a [5]={0};

initializes correctl

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 17:57

    int *a=new int[n];
    memset(a, 0, n*sizeof(int));
    

    That sets the all the bytes of the array to 0. For char * too, you could use memset. See http://www.cplusplus.com/reference/clibrary/cstring/memset/ for a more formal definition and usage.

提交回复
热议问题