Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While writing my code on Visual Studio 2022, I came across the error (E0028) that the expression must have a constant value in line 11.

#include <iostream>
using namespace std;

int main() 
{
	int n;
	
	cout<<"Enter"<<endl;
	cin>>n;
	
	int a[n];     //error line
	
	for(int i = 0; i<n; i++)
	{
	    cin>>a[i];
	}
	for(int i = 0; i<n; i++)
	{
	    cout<<" "<<a[i];
	}
	
	return 0;
}


But when I put the same code in any online compiler, it worked fine. How does this happen? Also how to resolve the issue in Visual Studio 2022.

What I have tried:

I think that the best way to deal with this is dynamic array allocation using vectors, but I would like to hear your views. Also, why don't we have the same error in online compilers?
Posted
Updated 10-Jun-22 3:53am

You cannot declare stack based arrays in that way, as the size needs to be known at compile time. Dynamic arrays must be created on the heap using the new directive, thus:
C++
int n;

cout<<"Enter"<<endl;
cin>>n;

int* a = new int[n]; // allocate n integer variables
 
Share this answer
 
Comments
CPallini 9-Jun-22 11:57am    
5.Remember to delete it. :-D
Richard MacCutchan 9-Jun-22 12:07pm    
In the above code it will only last a few seconds.
CPallini 9-Jun-22 13:07pm    
Nooo...................
The 'I'm confident in OS cleanup man'! :-D :-D :-D
Richard MacCutchan 10-Jun-22 2:57am    
Come on, it hardly matters in such a small sample.
merano99 10-Jun-22 16:29pm    
Better to use an STL container, no cleanup needed and resizable if needed.
In addition, range-based for loops are supported.
This page Variable Length (Using the GNU Compiler Collection (GCC))[^] says that VLA's are allowed in ISO C99 and are available in GNU C++, but MS C/C++ does not implement them, either for C or C++. If you need storage that is dependent on runtime, then its usually best to use a vector rather than a static array. You can always get a pointer to the underlying array using the data() member function. But you'd probably only need that if you need to pass the data off to a C library function.
 
Share this answer
 
Comments
CPallini 9-Jun-22 11:57am    
5.
I wouldn't expect VLAs to ever work with C++ in the standard since there are already containers in the STL that you can use for them. The GCC compilers currently offer it, although the standard does not provide for it.
Since the question is only tagged as C++ here, I would advise against using it as it's going off the standard path. Since VLAs have been available in C since C99 (1999), I wouldn't wait for it after more than 20 years in C++.

GCC had VLA as an extension before C99, one that also extends into its C++ dialect.
en.wikipedia.org/wiki/Variable-length_array

VLAs are C-specific and are not adopted by the C++ standard. C++ provides container classes that allow for variable fields.
de.wikipedia.org/wiki/Variable_Length_Array (german version)
C++
std::vector <int> a(n);     //error line

for (int i = 0; i<n; i++) {
	std::cin >> a[i];
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900