How do you initialise a dynamic array in C++?

前端 未结 10 2135
梦毁少年i
梦毁少年i 2020-11-27 13:27

How do I achieve the dynamic equivalent of this static array initialisation:

char c[2] = {};  // Sets all members to \'\\0\';

In other word

10条回答
  •  爱一瞬间的悲伤
    2020-11-27 13:50

    The array form of new-expression accepts only one form of initializer: an empty (). This, BTW, has the same effect as the empty {} in your non-dynamic initialization.


    The above applies to pre-C++11 language. Starting from C++11 one can use uniform initialization syntax with array new-expressions

    char* c = new char[length]{};
    char* d = new char[length]{ 'a', 'b', 'c' };
    

提交回复
热议问题