Is there any way in c to change the value of a global variable through a function without passing it to the function?
C Programming - Very basic and powerful High level language
In C there are two types of access scope for variables :
- Global scope - can be accessed throughout the program
- Local scope - can be accessed only inside a block where it is created
Now the need is to change the value of global variable without passing it to a function. Lets see how we are going to do this.
There are two ways to achieve this,
- Assigning value directly to global variable
- //including C standard input/output library
- #include
- //Global variable
- int a = 5;
- //declaring required function
- void change();
- //main program
- void main(){
- printf("\n Before function call => a = %d\n", a);
- change();
- printf("\n After function call => a = %d\n", a);
- }
- //defining the above declared function
- void change(){
- //just assigning 10 to a
- a = 10;
- }
Inside function change [Line 14 - 16], when [math]a[/math] is called it directly points to the global variable and assigns value to it.
Now let us consider next method.
- Using pointer concept
- //including C standard input/output library
- #include
- //Global variable
- int a = 5;
- //declaring required function
- void changeUsingPtr();
- //main program
- void main(){
- printf("\n Before function call => a = %d\n", a);
- changeUsingPtr();
- printf("\n After function call => a = %d\n", a);
- }
- //defining the above declared function
- void changeUsingPtr(){
- //pointer declared
- int *b;
- //address of variable a is assigned to b
- b = &a;
- //value present in address is incremented
- ++*b;
- }
Here a pointer is declared which holds the address of global variable and the value in the address is incremented after de-referencing, which forces to use pre-increment. Post increment doesn’t help on this.
The output remains the same for both case.
Output :
Before function call => a = 5
After function call => a = 10
In both cases, we need to ensure there is no conflicts between local and global variable. No local variable and global variable should be declared with same name.
So what if we stuck in the above scenario, having a conflict between two scope variables. Adding a small code block will help us cross the line. Hang on✋, Will show you how to make it.
- //including C standard input/output library
- #include
- //Global variable
- int a = 5;
- //function for returning the global variable address
- int* globalVarAddress(){
- return &a;
- }
- //declaring required function
- void changeUsingPtr();
- //main program
- void main(){
- printf("\n Before function call => a = %d\n", a);
- changeUsingPtr();
- printf("\n After function call => a = %d\n", a);
- }
- //defining the above declared function
- void changeUsingPtr(){
- //local variable using same name as global
- int a = 10;
- //pointer declared
- int *b;
- //fn call returns address of global a
- b = globalVarAddress();
- //value present in address is incremented
- ++*b;
- }
Output will be the same as above.
In Line 21, if we use [math]&a[/math] same as previous case then the address of local variable will be stored in [math]b[/math] since local variable always wins in local global conflicts. Using so will increment local variable [math]a[/math] to [math]11[/math] and global a remains unchanged.
Hope we are done with it.
[math]while(noSuccess){ tryAgain(); } - #Nk :) [/math][math] [/math]