I'm having a few issues tracking the values of global variables when there are local variables that exist with the same name.
当存在具有相同名称的局部变量时,我有几个跟踪全局变量值的问题。
This is the code I'm working with:
这是我正在处理的代码:
#include <stdio.h>
void func(int);
int x=6, y=7, z=10;
int
main(int argc, char **argv) {
int z=5;
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(x);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(y);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
func(z);
printf("main: x=%2d, y=%2d, z=%2d\n", x, y, z);
return 0;
}
void
func(int x) {
x = x+1;
y = y+1;
printf("func: x=%2d, y=%2d, z=%2d\n", x, y, z);
}
Note the globals have the same name as the locals in func(), except for z. When I run the program. I get the following output:
注意,全局变量的名称与func()中的本地名称相同,除了z,当我运行程序时。我得到如下输出:
main: x= 6, y= 7, z= 5
func: x= 7, y= 8, z=10
main: x= 6, y= 8, z= 5
func: x= 9, y= 9, z=10
main: x= 6, y= 9, z= 5
func: x= 6, y=10, z=10
main: x= 6, y=10, z= 5
I can understand where the first line comes from. It's just the values of the global variables, but main is using 5 rather than 10 because the global variable shadows the local variable. I can also understand line 2. global variable x is passed into func, giving a 7. Global variable y is used also, giving an 8.
我能理解第一行是怎么来的。它只是全局变量的值,但main使用的是5而不是10,因为全局变量阴影是局部变量。我也能理解第2行。全局变量x被传递到func,给出一个7。全局变量y也被使用,给出一个8。
Line 3 is where I lose track of values. Why has the value of global variable y stayed as 8? Did the func() function call change its value permanently? I thought this was not possible since y in func() is just a local variable. I understand where the x = 6 comes from in line 3 though.
第3行是我失去对值的跟踪的地方。为什么全局变量y的值是8?func()函数调用是否永久地改变了它的值?我认为这是不可能的,因为在func()中y只是一个局部变量。我知道x = 6在第3行的位置。
Line 4 I lose track even more. How can the value of x jump from 6 to 9?
第4行我失去了更多的线索。x的值如何从6跳到9?
It would be great if someone could walk me through this output, and perhaps give me a quick explanation on scope and also on shadowing.
如果有人能让我通过这个输出,也许能给我一个关于范围和阴影的快速解释,那就太好了。
2 个解决方案
#1
1
y
is not a local variable inside func()
. In order for it to be a local variable (which would shadow the global y
variable), you would have to declare another y
variable inside func()
, e.g. as in the example below. (This version will always print "y=1", and will not modify the global y
variable.)
y不是func()中的局部变量。为了使它是一个局部变量(它会影响全局变量y变量),你必须在func()中声明另一个y变量,例如在下面的例子中。(此版本将始终打印“y=1”,不会修改全局y变量。)
void
func(int x) {
int y = 0;
x = x+1;
y = y+1;
printf("func: x=%2d, y=%2d, z=%2d\n", x, y, z);
}
Since y
is not a local variable inside the original func()
, it's incrementing the global y
.
由于y不是原始func()中的一个局部变量,所以它增加了全局y。
Note that x
is local to func()
too, and is passed in by value. That is, a call func(foo)
will copy the value of foo
into the x
parameter, and changing the x
parameter inside func()
will not change foo
.
请注意,x是func()的本地函数,并按值传递。也就是说,调用func(foo)会将foo的值复制到x参数中,并且在func()中更改x参数不会改变foo。
Line 4 comes from the func(y)
call. It will set the (local) x
parameter inside func()
to the current value of the global variable y
. y
happens to have the value 8 at that point. (It was incremented from 7 to 8 by the previous call to func()
.) The assignment x = x+1
inside func()
bumps that to 9, which is then printed.
第4行来自func(y)调用。它将在func()中设置(本地)x参数到全局变量y的当前值。y恰好在那个点上有值8。(之前对func()的调用从7增加到8。)在func()中,赋值x = x+1,将其凸出到9,然后打印出来。
#2
2
To answer your first question
回答你的第一个问题。
Why has the value of global variable y stayed as 8? Did the func() function call change its value permanently?
为什么全局变量y的值是8?func()函数调用是否永久地改变了它的值?
Yes it did change the global variable y
permanently, because it is global and not local (it would be local if it was declared as int y;
)
是的,它确实永久地改变了全局变量y,因为它是全局变量,而不是本地变量(如果它被声明为int y,那么它将是本地的)。
How can the value of x jump from 6 to 9?
x的值如何从6跳到9?
You pass the global variable y
, which is changed to 8 in the first function call, to the function. This value is locally stored in the function as x
. So x + 1 will become 9.
将全局变量y传递给函数,在第一个函数调用中,y被更改为8。这个值在函数中作为x存储,所以x + 1会变成9。
This is the danger of global variables. Once you name local variables the same you WILL lose track of what value is contains what value.
这是全局变量的危险。一旦你命名了局部变量,你就会失去对什么值的跟踪。
A little example to explain some details:
一个小例子来解释一些细节:
int x = 3;
int main(){
int x = 8;
printf("%d", x);
}
This will print 8
because the code will grab the local value of x
.
这将打印8,因为代码将获取x的本地值。
Furthermore, when you change a global variable this will be remembered for the lifetime of the program.
此外,当您更改一个全局变量时,将会记住该程序的生命周期。
#1
1
y
is not a local variable inside func()
. In order for it to be a local variable (which would shadow the global y
variable), you would have to declare another y
variable inside func()
, e.g. as in the example below. (This version will always print "y=1", and will not modify the global y
variable.)
y不是func()中的局部变量。为了使它是一个局部变量(它会影响全局变量y变量),你必须在func()中声明另一个y变量,例如在下面的例子中。(此版本将始终打印“y=1”,不会修改全局y变量。)
void
func(int x) {
int y = 0;
x = x+1;
y = y+1;
printf("func: x=%2d, y=%2d, z=%2d\n", x, y, z);
}
Since y
is not a local variable inside the original func()
, it's incrementing the global y
.
由于y不是原始func()中的一个局部变量,所以它增加了全局y。
Note that x
is local to func()
too, and is passed in by value. That is, a call func(foo)
will copy the value of foo
into the x
parameter, and changing the x
parameter inside func()
will not change foo
.
请注意,x是func()的本地函数,并按值传递。也就是说,调用func(foo)会将foo的值复制到x参数中,并且在func()中更改x参数不会改变foo。
Line 4 comes from the func(y)
call. It will set the (local) x
parameter inside func()
to the current value of the global variable y
. y
happens to have the value 8 at that point. (It was incremented from 7 to 8 by the previous call to func()
.) The assignment x = x+1
inside func()
bumps that to 9, which is then printed.
第4行来自func(y)调用。它将在func()中设置(本地)x参数到全局变量y的当前值。y恰好在那个点上有值8。(之前对func()的调用从7增加到8。)在func()中,赋值x = x+1,将其凸出到9,然后打印出来。
#2
2
To answer your first question
回答你的第一个问题。
Why has the value of global variable y stayed as 8? Did the func() function call change its value permanently?
为什么全局变量y的值是8?func()函数调用是否永久地改变了它的值?
Yes it did change the global variable y
permanently, because it is global and not local (it would be local if it was declared as int y;
)
是的,它确实永久地改变了全局变量y,因为它是全局变量,而不是本地变量(如果它被声明为int y,那么它将是本地的)。
How can the value of x jump from 6 to 9?
x的值如何从6跳到9?
You pass the global variable y
, which is changed to 8 in the first function call, to the function. This value is locally stored in the function as x
. So x + 1 will become 9.
将全局变量y传递给函数,在第一个函数调用中,y被更改为8。这个值在函数中作为x存储,所以x + 1会变成9。
This is the danger of global variables. Once you name local variables the same you WILL lose track of what value is contains what value.
这是全局变量的危险。一旦你命名了局部变量,你就会失去对什么值的跟踪。
A little example to explain some details:
一个小例子来解释一些细节:
int x = 3;
int main(){
int x = 8;
printf("%d", x);
}
This will print 8
because the code will grab the local value of x
.
这将打印8,因为代码将获取x的本地值。
Furthermore, when you change a global variable this will be remembered for the lifetime of the program.
此外,当您更改一个全局变量时,将会记住该程序的生命周期。