Arduino函数变量值在函数调用之间变化

时间:2016-10-06 20:52:25

标签: c arduino

我有三个函数,每个函数调用next并传递一些值。奇怪的是,在我对第三个函数进行多次调用的第二个函数中,其中一个值在调用之间发生了变化。而这个变量肯定是本地的。我错过了什么吗?

void functionA(...){
  //Something...
  int i=1,j=1,k=2;
  functionB(i,j,k);
}

void functionB(int i,int j,int k){
  String X="";
  Serial.print(i);//Gives 1
  if(j==1)
    X="hello world";
  functionC(i,j,k,X);//Call 1 to functionC
  Serial.print(i);//Gives 0 !!!!!!!!!!!!!!!!!!!!!!!!
  if(j==2)
    X="hello world2";
  functionC(i,j,k,X);//Call 2 to functionC
}

void functionC(int i, int j, int k, String X){

   if(i) 
     //Do something
   else 
     //Do somethingelse

}

我的实验

void functionC(int i, int j, int k, String X){
       //Print i here. No difference with the result.
       if(i) 
         //Do something
       else 
         //Do somethingelse
       //But if I Print i here again, then it is working. As in, "i" does not change anymore. between each call to functionC
    }

1 个答案:

答案 0 :(得分:0)

你要么传递一些指针,要么函数C正在做一些带内存的时髦东西。除了这两个选项之外,我没有理由明白为什么变量中的值会像您的示例中那样简单地改变。

相关问题