I know this sound easy to you guys but I think this may be different
Here is my simple code in which i am getting error in declaring Array
#include<iostream>
using namespace std;
int top=-1,capacity=2;
int arr[capacity];
main(){
}//main
I know this sound easy to you guys but I think this may be different
Here is my simple code in which i am getting error in declaring Array
#include<iostream>
using namespace std;
int top=-1,capacity=2;
int arr[capacity];
main(){
}//main
A fixed length array needs a compile-time constant to declare its size. You need to declare capacity
as const
or constexpr
in order to use it in an array declaration, eg:
#include <iostream>
using namespace std;
int top = -1;
const int capacity = 2;
int arr[capacity];
main(){
}
If you want to define the capacity
at runtime then you need to use a dynamic array, like std::vector
, eg:
#include <iostream>
#include <vector>
using namespace std;
int top = -1, capacity;
vector<int> arr;
main(){
capacity = ...;
arr.resize(capacity);
}