永久改变参数值 [英] permanently changing value of parameter

查看:23
本文介绍了永久改变参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数检查,它接受一个布尔值作为参数,使用布尔值然后改变它.我总是给这个函数传递一个全局变量,比如:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆