Casa > I > Is There Any Way In C To Change The Value Of A Global Variable Through A Function Without Passing It To The Function?

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 :

  1. Global scope - can be accessed throughout the program
  2. 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
  1. //including C standard input/output library 
  2. #include  
  3.  
  4. //Global variable 
  5. int a = 5; 
  6.  
  7. //declaring required function 
  8. void change(); 
  9.  
  10. //main program 
  11. void main(){ 
  12. printf("\n Before function call => a = %d\n", a); 
  13. change(); 
  14. printf("\n After function call => a = %d\n", a); 
  15.  
  16. //defining the above declared function 
  17. void change(){ 
  18. //just assigning 10 to a 
  19. 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
  1. //including C standard input/output library 
  2. #include  
  3.  
  4. //Global variable 
  5. int a = 5; 
  6.  
  7. //declaring required function 
  8. void changeUsingPtr(); 
  9.  
  10. //main program 
  11. void main(){ 
  12. printf("\n Before function call => a = %d\n", a); 
  13. changeUsingPtr(); 
  14. printf("\n After function call => a = %d\n", a); 
  15.  
  16. //defining the above declared function 
  17. void changeUsingPtr(){ 
  18.  
  19. //pointer declared 
  20. int *b; 
  21.  
  22. //address of variable a is assigned to b 
  23. b = &a; 
  24.  
  25. //value present in address is incremented 
  26. ++*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.

  1. //including C standard input/output library 
  2. #include  
  3.  
  4. //Global variable 
  5. int a = 5; 
  6.  
  7. //function for returning the global variable address 
  8. int* globalVarAddress(){ 
  9. return &a; 
  10.  
  11. //declaring required function 
  12. void changeUsingPtr(); 
  13.  
  14. //main program 
  15. void main(){ 
  16. printf("\n Before function call => a = %d\n", a); 
  17. changeUsingPtr(); 
  18. printf("\n After function call => a = %d\n", a); 
  19.  
  20. //defining the above declared function 
  21. void changeUsingPtr(){ 
  22.  
  23. //local variable using same name as global 
  24. int a = 10; 
  25.  
  26. //pointer declared 
  27. int *b; 
  28.  
  29. //fn call returns address of global a 
  30. b = globalVarAddress(); 
  31.  
  32. //value present in address is incremented 
  33. ++*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]

De Baun

Como saber se alguém está acessando meu roteador e usando minha Internet remotamente :: Devo usar uma VPN na minha casa ou rede móvel?