简体   繁体   English

退出函数后,为什么此变量值返回默认值?

[英]Why does this variable value return to default value after exiting the function?

I want to move the knight in a chess program. 我想用国际象棋程序移动骑士。 For this reason, I have these two variables( currentRow and currentColumn ) defined on top of all functions including main. 因此,我在包括main在内的所有函数的顶部定义了这两个变量( currentRowcurrentColumn )。 (I did this because I wanted these variables as global variables to all functions) as below. (这样做是因为我希望这些变量作为所有函数的全局变量),如下所示。 Because when the knight moves, its position will change. 因为当骑士移动时,它的位置会改变。 And this will be the input to its next move. 这将是下一步行动的输入。

What I don't understand is when I debug, I saw that these variables are changing in the function but as soon as it exits function, they return to their default values (3 and 4). 我不了解的是,当我调试时,我看到这些变量在函数中发生了变化,但是一旦退出函数,它们就会恢复为默认值(3和4)。

Can you tell me how to fix this? 你能告诉我如何解决这个问题吗? Thanks in advance... 提前致谢...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int currentRow=3;
int currentColumn=4;

int main(void){

...
}

int checkIsEmptyandMoveAccordingly(int moveNumber, int currentRow, int currentColumn){

   if (chessBoard[currentRow+vertical[moveNumber]][currentColumn+horizontal[moveNumber]]==0   && currentRow+vertical[moveNumber]>=0 && currentColumn+horizontal[moveNumber] >=0   ){   //if empty,move to new location

         currentRow+=vertical[moveNumber];
         currentColumn+=horizontal[moveNumber];
         printf("Move randomised to: %d\n", moveNumber);
         printf("Knight has moved to chessBoard[%d][%d].\n",currentRow,currentColumn);
         count++;
         printf("Move count is %d.\n",count);
         chessBoard[currentRow][currentColumn]=1;
         if(!checkIsAroundFUll()){
            moveNumber=randomiseMovement();
            return moveNumber;
         }
         else   { 
              printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!\n");
              return -1;
         }
   }

   else if (chessBoard[currentRow+vertical[moveNumber]][currentColumn+horizontal[moveNumber]]==1)  {                                                                                                                                                    //if not empty, randomise again

         printf("Knight CAN NOT MOVE! Target SPACE IS OCCUPIED\n");
         if(!checkIsAroundFUll()){
            moveNumber=randomiseMovement();
            return moveNumber;
        }
         else   { 
              printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!");
              return -1;
         }

   }

   else {
         printf("OUT OF BOUNDS!! CAN NOT MOVE. TRYING ANOTHER MOVEMENT");
         if(!checkIsAroundFUll()){
            moveNumber=randomiseMovement();
            return moveNumber;
        }
         else   { 
              printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!");

              return -1;
         }
   }
}

int currentRow, int currentColumn are in the function parameter list, so they are local variables. int currentRow, int currentColumn在函数参数列表中,因此它们是局部变量。 They are hiding the global ones with the same names. 他们隐藏了具有相同名称的全局名称。

Your function has new variables currentRow and currentColumn declared as parameters to the function. 您的函数有新变量currentRow和currentColumn声明为该函数的参数。 If you want to update the global variables, remove these parameters (and don't pass them when you call the function) and you should see the globals update. 如果要更新全局变量,请删除这些参数(并且在调用函数时不要传递它们),您应该会看到全局更新。

What you're doing is shadowing the global variables. 您正在做的事情正在遮盖全局变量。 With the right compiler warning enabled (which varies by compiler) you would be told about this error. 启用正确的编译器警告(具体取决于编译器),系统将告知您此错误。

Try compiling with -Wall -Werror if you are using gcc. 如果使用gcc,请尝试使用-Wall -Werror进行编译。

Your function is changing local copies. 您的功能是更改本地副本。 When you pass them to the function, they pass by value, the function creates local copies, and the local scope overrides the global scope. 当您将它们传递给函数时,它们将按值传递,函数将创建本地副本,并且本地范围将覆盖全局范围。 If you want to reference global variables, don't pass them into your function, just access them from there. 如果要引用全局变量,请不要将其传递到函数中,而应从那里访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM