-1
#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    int arr[n];

    return 0;
}

here it wont let define the arr of size n the value of n is defined above so n is most propbably a constant right?

  • 1
    https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – NathanOliver Jul 31 '22 at 02:40
  • Your code declares and reads a *variable* (a named object with value that can change as the program is running) not a constant. `int arr[n]` where `n` is a variable is not valid in standard C++. The requirement is that `n` would have a value that can be computed during compilation (known as a compile-time constant). – Peter Jul 31 '22 at 03:26

1 Answers1

0

n is not a constant. In C++, n would only be a constant if it is defined as const int n.

If you're reading n from cin with cin >> n, then how can it be a constant? You can't control what the user inputs.

Ryan Zhang
  • 1,856
  • 9
  • 19
  • It is correct what you say but its not that relevant for the question, because also a const variable is not allowed for array size, it must instead be a compile time constant. – gerum Jul 31 '22 at 07:54