user3837245
user3837245

Reputation: 99

How to use global variables in function?

I have a global variable, i want to use it in my function without parameter. Example:

void main(){
int a[100];
earray();
}
void earray(){
//i want to use a[] here.
}

Upvotes: 0

Views: 136

Answers (2)

Alfie J. Palmer
Alfie J. Palmer

Reputation: 837

It's not a global variable (it's local). I assume you're trying to achieve something like the following:

Pass the local value to the function as a parameter (better practice)

For example :

void earray(int array[]){
    //array.
}

void main(){
    int a[100];
    earray(a);
}

Or, as a Global Variable (as you mentioned):

int a[100];

void earray(){
    //a.
}

void main(){
    earray();
}

Upvotes: 7

4386427
4386427

Reputation: 44256

That is not a global variabel when you declare it in main. Move it out of main like this:

int a[100];  // Global variable

void main(){
earray();
}
void earray(){
//i want to use a[] here.
}

Upvotes: 1

Related Questions