Using const int as array size

前端 未结 5 694
太阳男子
太阳男子 2020-12-21 18:59

Why am I able to use a locally declared const int as the size of an array declaration but am not allowed to do the same with a const int passed as

5条回答
  •  再見小時候
    2020-12-21 19:53

    Array size should be known at compile time.

    const int with local variables may do not work neither if the value is not known at compile time as:

    void f2(){
      const int dim = bar();
      int nums[dim];  // error
    }
    

    In Both case, const int tells that the value doesn't change, not that is it known at compile time.

提交回复
热议问题