I want to define a global array (used in other functions) based on input main (); (specific array size). The extern keyword did not help.
#include <iostream> using namespace std; void gen_sieve_primes(void); int main() { int MaxNum; cin >> MaxNum; int *primes = new int[MaxNum]; delete[] primes; return 0; } //functions where variable MaxNum is used
You declare it outside the main:
int maxNum; int main() { ... }
Ideally, you do not do this at all. Globals are rarely useful, and hardly ever (or rather: never) are needed.
Just define it in a global area
int MaxNum; int main(){ cin >> MaxNum; }
Declare an array outside the main functional brackets.
#include <iostream> using namespace std; void gen_sieve_primes(void); (Declare the variables here!) int main() { extern int MaxNum; cin >> MaxNum; int *primes = new int[MaxNum]; delete[] primes; return 0; } //functions where variable MaxNum is used