How to convert int to const int to assign array size on stack?

前端 未结 2 1323
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 10:01

I am trying to allocate a fixed size on stack to an integer array

#include
using namespace std;

int main(){

    int n1 = 10;
          


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-13 10:18

    Only fixed-size arrays can be allocated that way. Either allocate memory dynamically (int* foo = new int[N];) and delete it when you're done, or (preferably) use std::vector instead.

    (Edit: GCC accepts that as an extension, but it's not part of the C++ standard.)

提交回复
热议问题