永久更改参数的值 [英] permanently changing value of parameter
本文介绍了永久更改参数的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个功能检查接受一个布尔值作为参数,采用布尔值,然后改变它。我总是向这个函数传递一个全局变量,如:
检查(4 some_global_boolean,'A');
和我想要换到的变量保持检查后调用。眼下,这种变化不会超出这个函数调用。我该怎么做呢?
布尔检查(INT键,布尔prev_key_high,焦炭C){ 如果(键== LOW){
如果(prev_key_high){
prev_key_high = FALSE;
Serial.println(C);
返回true;
}
} 其他{
prev_key_high = TRUE;
返回false;
}
}
解决方案
您应该传递指针,但你必须改变所有用途为取消引用指针是这样的:
布尔检查(INT键,布尔* prev_key_high,焦炭C){ ...
如果(* prev_key_high)
...
* prev_key_high = TRUE;
...
I have a function check that takes a boolean as an argument, uses the value of the boolean and then changes it. I always pass this function a global variable, like:
check(4, some_global_boolean, 'a');
and I want the change to the variable to remain after "check" is called. Right now, the change is not going beyond this function call. How do I do this?
boolean check(int key, boolean prev_key_high, char c){
if (key == LOW) {
if ( prev_key_high){
prev_key_high = false;
Serial.println(c);
return true;
}
}
else {
prev_key_high = true;
return false;
}
}
解决方案
You should pass the pointer, but you'll have to change all usages to dereferenced pointer like this:
boolean check(int key, boolean *prev_key_high, char c){
...
if (*prev_key_high)
...
*prev_key_high = true;
...
这篇关于永久更改参数的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文