Is it possible to return a link to a static variable?

I wanted to know if there are any side effects under any circumstances.

For ex:

Ex1: void* func1() { void* p_ref = NULL; //function scope static variable static int var1 = 2; p_ref = &var1; return p_ref; } Ex2: //file scope static variable static int var2 = 2; void* func2() { void* p_ref = NULL; var2 = 3; p_ref = &var2; return p_ref; } 

So, in the above two cases, what is the difference, besides the fact that var1 is a function area and var2 is a file area.

Thanks in advance.

+4
source share
2 answers

I do not believe that there is any difference. They are both global variables, just the name of the first is visible only inside the scope of the func1 function.

+2
source

Essentially, there is no difference other than an area.

Therefore, a local variable is preferable if this pointer is the only way to access the variable.

+2
source

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


All Articles