can we give size of static array a variable

前端 未结 6 446
余生分开走
余生分开走 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 11:56

    What you have is called a variable-length array (VLA), and it is not part of C++, although it is part of C99. Many compilers offer this feature as an extension.

    Even the very new C++11 doesn't include VLAs, as the entire concept doesn't fit well into the advanced type system of C++11 (e.g. what is decltype(array)?), and C++ offers out-of-the box library solutions for runtime-sized arrays that are much more powerful (like std::vector).

    In GCC, compiling with -std=c++98/c++03/c++0x and -pedantic will give you a warning.

提交回复
热议问题