0

I know this question has been asked many times before. But everything I went through indicated it is not possible to declare a variable size array at run time unless we use pointers or vectors. But I am always able to take the array size input in a variable(say n) from user and then declare a array of size n. It never gives any warning or error in codeblocks ,vs code or for that matter any coding website.

   int n;
   cin>>n;

   int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];

    }  

My this code always works, but i don't understand How!!!! Here is my Build message

Thanks in Advance

Sourabh Yadav
  • 31
  • 1
  • 6
  • 3
    It works because the compiler you are using supports [VLAs](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html) as an extension. VLAs are not part of the portable C++. – Ron May 21 '20 at 12:19
  • 1
    @Ron - IMHO, you should expand your comment in an answer – max66 May 21 '20 at 12:20
  • 1
    *t never gives any warning or error in codeblocks ,vs code or for that matter any coding website.* -- Try Visual Studio using Visual C++, and you will see the error. [See this](https://godbolt.org/z/rgPRvU) – PaulMcKenzie May 21 '20 at 12:20
  • 1
    @max66 Appreciate it. There seems to be a duplicate already. – Ron May 21 '20 at 12:27

0 Answers0