can we give size of static array a variable

前端 未结 6 471
余生分开走
余生分开走 2021-01-04 11:21

hello every one i want to ask that i have read that we can declare dynamic array only by using pointer and using malloc or newlike

int * array = new int[strl         


        
6条回答
  •  攒了一身酷
    2021-01-04 12:07

    int array[strlen(argv[2])];
    

    It is certainly not valid C++ Standard code, as it is defining a variable length array (VLA) which is not allowed in any version of C++ ISO Standard. It is valid only in C99. And in a non-standard versions of C or C++ implementation. GCC provides VLA as an extension, in C++ as well.

    So you're left with first option. But don't worry, you don't even need that, as you have even better option. Use std::vector:

    std::vector array(strlen(argv[2])); 
    

    Use it.

提交回复
热议问题