Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining global variable in main()

I want to define global array (used in other functions) based on input from main(); (concretely array size). The extern keyword didn't 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
like image 244
sjaustirni Avatar asked Jan 14 '23 21:01

sjaustirni


1 Answers

Just define it in global scope

int MaxNum;
int main(){
    cin >> MaxNum;
}
like image 51
RiaD Avatar answered Jan 27 '23 18:01

RiaD