How to copy a local variable to a global variable if both have the same name in C without using a third variable?

I have a global variable and a local variable with the same name. Can I copy a local variable into a global variable (destination) without first assigning the value of the global variable to some temporary variable (using extern), and then assigning the temporary variable to the global variable? I need to do something like this:

#include<stdio.h> int myVariable = 50; void myFunction() { int myVariable; myVariable /*global*/ = myVariable /*local*/; } 

Is there any way in C for this (without using temporary variables (or pointers in case of arrays))? I found this possible in C ++, Java, or C # using keywords like super, base, etc., but couldn't find a solution in C.

I already mentioned How to access a shadow global variable in C?

+6
source share
8 answers

There is no way in C99 to specify the specific variable / constant region that you want to use. It will automatically refer to the internal scope when accessing a variable / constant.

From standard C99 6.2.1.4 :

... If the identifier denotes two different objects in the same namespace, the scope may overlap. If so, the volume of one object (internal volume) will be a strict subset of the scope of another object (external coverage). Inside the inner region, an identifier denotes an object declared in the inner region; an object declared in the outer region is hidden (and not visible) in the inner region ...

So how do I get around this?

Simple, change the name of the innermost scope variable :)

+13
source

This is terrible, but in GNU C you can do it with

 * ({ extern int myVariable; &myVariable; }) = myVariable; 

Think about this, which can serve in standard C with a helper function:

 static int * GlobalMyVariable(void) { return &myVariable; } … *GlobalMyVariable() = myVariable; 

Of course, you did not request temporary variables, and this does not use a temporary variable, but at first glance it adds “stuff” to your executable file. However, I compiled a simple use of this with optimization, and the helper function completely disappeared in the generated assembler code. Thus, it is not superfluous code or data when the compiler is optimized correctly.

+6
source

Is the structure considered as another variable?

 #include<stdio.h> struct Global { int myVariable; } g; void myFunction() { int myVariable = 50; g.myVariable /*global*/ = myVariable /*local*/; } 

This should do what you ask.

+1
source

Add a global pointer int int.

OP: How to copy a local variable to a global variable if both have the same array names in C without using a third variable?
I cheated and do not use a third variable, but a constant. (did I sneak through the door?)

OP: without first assigning the value of the global variable to any temporary variable (using extern) and then assigning the temporary variable to the global variable?
Appears to pass this test - a temporary variable is not used.

OP: somehow in C, do this (without using temporary variables (or pointers in case of arrays))?
Well, this is not a temporary use, since global variables are permanent. And do not use a pointer through an array. It uses a pointer.

 int myVariable = 50; int *const myVariableAddress = &myVariable; void myFunction() { // Could be here instead with // static int *const myVariableAddress = &myVariable; { int myVariable; *myVariableAddress = myVariable /*local*/; } } 
+1
source

This works, but the code must be in a common object or the program must be compiled so that the executable can see its own global characters (the -rdynamic flag is used in GCC):

 int myVariable = 50; void myFunction () { int myVariable = 10; *(int *)dlsym(0, "myVariable") = myVariable; } 
+1
source

1) use the return value:

 #include<stdio.h> int myVariable = 50; int myFunction1() { int myVariable; return myVariable; } 

2) use a pointer:

 void myFunction2( int * pp) { int myVariable; *pp = myVariable; } 

Using:

 int main(void) { myVariable = myFunction1(); /* or ... */ myFunction2 ( &myVariable); return 0; } 

}

-1
source
 ::myGlobal = myLocal 

This should have access to the variable from the global scope.

-1
source

What you are doing is “shading” the global variable. To undo it in a local block, declare it in your own nested block using extern.

  void myFunction() { int myVariable { extern int myVarible; //modify myVariable global here; } //modify myVariable localhere } 
-2
source

Source: https://habr.com/ru/post/951750/


All Articles