C语言 在 C 中更新全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18956628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating a global variable in C
提问by user2466382
I have a beginners C question. I want in the code below...
我有一个初学者 C 问题。我想在下面的代码...
include <stdio.h>
void iprint();
int i=0;
int main()
{
int j;
for (j=0; j<50; j++)
{
iprint(i);
printf("%d\n",i);
}
}
void iprint(i)
{
i +=1;
//printf("%d\n",i);
}
... the function "iprint" to update the value of i each time is it called, e.g. update i so that it can be used in main with the value 1 for iteration 2, and 3 for iteration 2 etc.
...每次调用时更新 i 值的函数“iprint”,例如更新 i 以便它可以在 main 中使用,值为 1 用于迭代 2,值为 3 用于迭代 2 等。
I accomplished this by changing the code to this:
我通过将代码更改为:
include <stdio.h>
int iprint();
int i=0;
int main()
{
int j;
for (j=0; j<50; j++)
{
i= iprint(i);
printf("%d\n",i);
}
}
int iprint(i)
{
i +=1;
//printf("%d\n",i);
return(i);
}
Do i have to return(i) to make that happen? The reason for asking, is that if i have a lot of functions using i, it's a bit annoying having to pass i between them. If you instead, somehow could update i like you update a global variable in matlab, that'd be great. Is it possible?
我必须返回(i)才能实现吗?问的原因是,如果我有很多使用 i 的函数,那么在它们之间传递 i 有点烦人。相反,如果你能以某种方式更新我,就像你在 matlab 中更新一个全局变量一样,那就太好了。是否可以?
回答by Some programmer dude
The problem with the first is that you pass the variable as an argument to the function, so when the function modifies the variable it's only modifying its own local copy and not the global variable. That is, the local variable ishadowsthe global variable i.
第一个问题是你将变量作为参数传递给函数,所以当函数修改变量时,它只修改它自己的本地副本,而不是全局变量。也就是说,局部变量会i影响全局变量i。
Not to mention that you don't actually declarethe argument properly, so your program should not even compile.
更不用说您实际上没有正确声明参数,因此您的程序甚至不应该编译。
回答by Vamsavardhana Vijay
Use a pointer to point to the global variable. Change the pointer value. Thats it
使用指针指向全局变量。更改指针值。就是这样
回答by Klas Lindb?ck
You don't need to pass global variables as parameters. If you declare a parameter or local variable with the same name as the global variable you will hide the global variable.
您不需要将全局变量作为参数传递。如果你声明一个与全局变量同名的参数或局部变量,你将隐藏全局变量。
include <stdio.h>
void iprint();
int i=0;
int main()
{
int j;
for (j=0; j<50; j++)
{
iprint();
printf("%d\n",i);
}
}
void iprint()
{
i +=1; /* No local variable i is defined, so i refers to the global variable.
//printf("%d\n",i);
}
回答by Himanshu Pandey
you could have incremented the value of i in main function itself. By the way change the function to
您可以在主函数本身中增加 i 的值。顺便把函数改成
int iprint(int i){
/*you have to mention the type of arguemnt and yes you have to return i, since i
variable in this function is local vaiable when you increment this i the value of
global variable i does not change.
*/
return i+1;
}
the statement
该声明
i=iprint(i); //this line updates the value of global i in main function
This happens like this because you are passing value in function by 'pass by value' method where a copy of variable is made. When you increment the i iprint method copy of global variable i is incremented. Global variable remains intact.
发生这种情况是因为您通过“按值传递”方法在函数中传递值,其中创建了变量的副本。当您增加 i 全局变量 i 的 iprint 方法副本时, i 增加。全局变量保持不变。
回答by Manav Akela
Must try this code
一定要试试这个代码
#include <stdio.h>
int i=0;
void iprint()
{
i =i+1;
//printf("%d\n",i);
}
int main()
{
int j;
for (j=0; j<50; j++)
{
iprint();
printf("%d\n",i);
}
}

