Well trying to conserve memory I'm trying to get my program to only make an array of what is needed nothing more. I've already figured out that the only variable i could use to set how many there is in a array is a const but according the the compiler i use, Visual C++ 2008 Express Edition, it needs to know how big the array is going to be before it's compiled...

Simply, this code:

#include<iostream>
using namespace std;
void main()
{
	int c;
	cin >> c;
	const int a=c;
	int b[a];
	system("pause");
}

Gives me these errors:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'b' : unknown size

If this is already solved on the site please give me the link because i had absolutely no idea on what to search for to find the solution for this...

Recommended Answers

All 8 Replies

The compiler complains because you need to initialize const int a.
Const variable are static binded by computer, so you cannot
change its value during runtime. If the above code was allowed
then you would be able to const variables during runtime. And this
would violate const data variable's purpose, to stay const.

To fix it you can do these :

const int MAX_SIZE = 10;
int Array[MAX_SIZE] = {0};

or

int MaxSize = 0;
cin >> MaxSize;
int Array = new int[MaxSize];

or just simply use vectors or deque.

The compiler complains because you need to initialize const int a.
Const variable are static binded by computer, so you cannot
change its value during runtime. If the above code was allowed
then you would be able to const variables during runtime. And this
would violate const data variable's purpose, to stay const.

To fix it you can do these :

const int MAX_SIZE = 10;
int Array[MAX_SIZE] = {0};

or

int MaxSize = 0;
cin >> MaxSize;
int Array = new int[MaxSize];

or just simply use vectors or deque.

So there is no way around it?...well if that is so that's all i need to know

Well this snippet

int s = 0;
cin >> s;
int A = new int[s];

Lets user input the size of the array. You can use A as if its
were declared like int A[10];

Just don't forget to use delete [] A, when done using it.

Well this snippet

int s = 0;
cin >> s;
int A = new int[s];

Lets user input the size of the array. You can use A as if its
were declared like int A[10];

Just don't forget to use delete [] A, when done using it.

Well whatever compiler u are using it's not the same as mine or I'm doing something wrong.
Code:

#include<iostream>
using namespace std;
void main()
{
	int s = 0;
	cin >> s;
	int A = new int[s];
	system("pause");
}

Gives me the error:

error C2440: 'initializing' : cannot convert from 'int *' to 'int'

Oh sorry, its suppose to be :

int  *A = new int[s];

Also int main(), and cin.get(), not void main, system("pause");

Oh sorry, its suppose to be :

int  *A = new int[s];

Thanks it works now.

Also int main(), and cin.get(), not void main, system("pause");

Personal preference...or is it just me because i was taught by a teacher who was learning c++ along with the class...idk

Personal preference...or is it just me because i was taught by a
teacher who was learning c++ along with the class...idk

Wow, its not a good idea for teacher to teach a subject when
he/she leaning with the class.

void main is not standard. System ("pause") is a bad command
to use. Either a get a better IDE or using cin.get(). Which is better
than system("pause")

Okimdone,
Code at OP is now supported by C99 standard compilers. C99 standard support not implemented in Visual C++ Express Edition. You should have to use c99 compatible mingw compiler.

Read this article - http://en.wikipedia.org/wiki/C99

Another informative link is - http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.1

SUMMARY:
int main()

main() must return int. Not void, not bool, not float. int. Just int, nothing but int, only int.

Some compilers accept void main(), but that is non-standard and shouldn't be used. Instead use int main().

void main()  // ← BAD; DO NOT DO THIS
 {
   ...
 } 

 int main()  // ← GOOD
 {
   ...
 } 

 int main(int argc, char** argv)  // ← GOOD
 {
   ...
 }
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.