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

前端 未结 10 2102
梦毁少年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:58

    No internal means, AFAIK. Use this: memset(c, 0, length);

    0 讨论(0)
  • 2020-11-27 13:59

    you have to initialize it "by hand" :

    char* c = new char[length];
    for(int i = 0;i<length;i++)
        c[i]='\0';
    
    0 讨论(0)
  • 2020-11-27 14:01
    char* c = new char[length]();
    
    0 讨论(0)
  • 2020-11-27 14:04

    and the implicit comment by many posters => Dont use arrays, use vectors. All of the benefits of arrays with none of the downsides. PLus you get lots of other goodies

    If you dont know STL, read Josuttis The C++ standard library and meyers effective STL

    0 讨论(0)
提交回复
热议问题