How can i declare an array with variable size? i dont want it to be dynamic array, it have to be static. I mean the following:

int size;
cin>>size;
float array[size];

the previous code when compiled gives me that a constant expression required for the array declaration, although an integer value is already assigned to "size" by "cin" ...
So is there a solution so i can modify the previous code so it'll work?? (without using a dynamic array)..
Appreciated..

Recommended Answers

All 2 Replies

The size of the array has to be known when compiling. That's why it must be a constant. The only way around this (that I know of) is allocating the array dynamically.

Why does it HAVE to be static? Is this an assignment where you need to puzzle out some way of doing this? gallas is right, memory allocation is for just such situations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.