C语言 如何在C中的另一个函数中使用局部变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28939904/
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
How to use values of local variable in another function in C
提问by Aircraft
Suppose there are two function i.e., function1()and function2()
假设有两个函数 iefunction1()和function2()
int function1()
{
int F;
printf("enter any value");
scanf("%d",&F);
printf("Value is %d",F);
}
In function1()variable Fis used which is local variable. How can I use the value of this local variable Fin another function function2()?
在function1()变量F中使用的是局部变量。如何F在另一个函数中使用这个局部变量的值function2()?
回答by Edwin Buck
Actually, you already did.
事实上,你已经做到了。
passing the reference of Fto scanfis how to use it. Writing your own function, you only need to decide if you are passing the reference or the value. If passing the value
传递Fto的引用scanf是如何使用它。编写自己的函数,您只需要决定是传递引用还是传递值。如果传递值
int function(int value) {
...
}
which would be called like
这将被称为
function(F);
if passing the reference
如果传递引用
int function(int* value) {
...
}
which would be called like
这将被称为
function(&F);
回答by Sourav Ghosh
Function local variable lifetimes are local to that function. If you have to use a value held by a local variable inside a function to another function, you can
函数局部变量生命周期对于该函数来说是局部的。如果必须将函数内部的局部变量保存的值用于另一个函数,则可以
returnthe value fromfunction1().- collect the return value in the caller function.
- pass the stored value as argument to
function2().
return值来自function1().- 在调用者函数中收集返回值。
- 将存储的值作为参数传递给
function2().
NOTE: above answer assumes that both function1()and function2()are called from a parent function, maybe func().
注意:上面的答案假设function1()和function2()都是从父函数调用的,也许func().
Edit:
编辑:
if you need to use multiple values of local variables in other functions, (without using global variables), most common practice is to
如果你需要在其他函数中使用多个局部变量的值,(不使用全局变量),最常见的做法是
- pass a pointer to structure as argument of the
function1() - fill the member element values inside
function1()from the local variables. - pass the structure (or, address of the structure, is needed) again to
function2()as argument.
- 将指向结构的指针作为参数传递
function1() function1()从局部变量填充成员元素值。- 再次将结构(或结构的地址)传递给
function2()作为参数。
回答by SMA
define another function which accepts input and pass it to function2 like:
定义另一个接受输入并将其传递给 function2 的函数,例如:
//main function
int f = function1();//return F from function1
function2(f);
Or if you can call function2 from function1, then you could do something like:
或者,如果您可以从 function1 调用 function2,那么您可以执行以下操作:
//function1
function2(F);
//function1 cond..
回答by Shubham Chaurasia
Well, you can not do so explicitly because the scope and lifetime of local variables are limited to that function.
好吧,您不能明确地这样做,因为局部变量的范围和生命周期仅限于该函数。
But if you want the variable of a F outside as well, the simplest possible solution is to make it global.
但是,如果您还希望将 F 的变量置于外部,那么最简单的可能解决方案是将其设为全局变量。
Secondly you can also pass F to another function as paramater.
其次,您还可以将 F 作为参数传递给另一个函数。
回答by chmike
It is not possible for function2 to use the local variable in function1 because the local variable is destroyed when the function1 returns. The solution is to return the value f as the result of function1.
function2 无法使用 function1 中的局部变量,因为在 function1 返回时局部变量被销毁。解决方案是返回值 f 作为 function1 的结果。
int function1()
{
int f;
printf("enter any value");
scanf("%d", &f);
printf("Value is %d", f);
return f; // <-- return the value in local variable f
}
void function2(int f) // <-- pass f as argument to function
{
printf("Value is %d", f); // <-- use f inside function2
}
int main(int argc, char* argv[])
{
int f = function1(); // <-- store value returned by function1 in f
...
function2(f); // <-- call function2 by passing f as argument
...
function2(function1()); // <-- passing f directly from function1 to function2
return 0;
}

