简体   繁体   English

为什么这个变量在传递给函数时会发生变化

[英]Why does this variable change when passed to a function

I've recently started working on a chess engine, and am having trouble with a function to apply moves to the board.我最近开始研究国际象棋引擎,但在将动作应用于棋盘的功能方面遇到了麻烦。 I have a structure type for the board state, and another to store moves.我有一个用于棋盘状态的结构类型,另一个用于存储移动。 These are both in my header file below.这些都在我下面的头文件中。

#ifndef DECLARATIONS_H
#define DECLARATIONS_H

#include <stdbool.h>

#define BOARD_SIZE 120

typedef struct move {
    int startingSquare;
    int endingSquare;
    int promotionPiece;
} move;

typedef struct boardState {
    int board[BOARD_SIZE];
    bool whoseTurn;
    int ply;
    int threeMoveRuleCount;
    int fiftyMoveRuleCount;
    bool whiteCastleKingside;
    bool whiteCastleQueenside;
    bool blackCastleKingside;
    bool blackCastleQueenside;
    int enPassant;
    float evaluation;
    move previousMove;
} boardState;

boardState currentBoard;

#endif

Main.c calls the "Process Move" function from movegeneration.c. Main.c 从 movegeneration.c 调用“Process Move”函数。 It passes one of the boardstate types and one of the move types:它传递一种 boardstate 类型和一种移动类型:

#include <stdio.h>
#include <string.h>
#include "declarations.h"

int main () {

    move firstMove;
    firstMove.startingSquare = 35;
    firstMove.endingSquare = 55;
    firstMove.promotionPiece = 0;

    printf("Value of firstMove.endingSquare: %d\n", firstMove.endingSquare);
    printf("Value of firstMove.startingSquare: %d\n", firstMove.startingSquare);
    printf("Value of firstMove.promotionPiece: %d\n", firstMove.promotionPiece);
    processMove(currentBoard, firstMove);

    return 0;
}

Below is movegeneration.c, which contains the function to process the move:下面是 movegeneration.c,其中包含处理移动的函数:

#include <stdio.h>
#include "declarations.h"

boardState processMove(boardState, move);

boardState processMove(boardState oldBoardState, move moveToApply) {

    boardState newBoardState = oldBoardState;
    printf("Value of moveToApply.endingSquare: %d\n", moveToApply.endingSquare);
    printf("Value of moveToApply.startingSquare: %d\n", moveToApply.startingSquare);
    printf("Value of moveToApply.promotionPiece: %d\n", moveToApply.promotionPiece);
    return newBoardState;
}

And here's the program output:这是程序输出:

Value of firstMove.endingSquare: 55
Value of firstMove.startingSquare: 35
Value of firstMove.promotionPiece: 0
Value of moveToApply.endingSquare: 0
Value of moveToApply.startingSquare: 55
Value of moveToApply.promotionPiece: 1996058613

This isn't all the code from the .c files, but it's everything relevant here.这不是 .c 文件中的所有代码,但它是所有相关的内容。 What makes this bizarre is that the values of the move change as soon as I call the function.奇怪的是,一旦我调用该函数,移动的值就会发生变化。 When I put all the code into one large .c file and compile that way, the problem goes away, but I'm trying to keep the code separate to stay organized.当我将所有代码放入一个大的 .c 文件并以这种方式进行编译时,问题就消失了,但我试图将代码分开以保持井井有条。 This one has me completely stumped.这个让我完全难住了。 I'd appreciate any help!我很感激任何帮助!

There's at least 2 problems here, causing undefined behaviour.这里至少有两个问题,导致未定义的行为。

Firstly, there are multiple definitions of boardState currentBoard;首先, boardState currentBoard;有多种定义boardState currentBoard; . . Remember that #include is a plain text replacement, so both of your c files end up with a definition of that object.请记住, #include是纯文本替换,因此您的两个c文件都以该对象的定义结束。 See here for an in-depth explanation with examples .有关示例的深入解释,请参见此处

If you want to have a global object accessible from multiple translation units then the header file should contain a declaration only (ie prepend extern to the current line), and exactly one of the .c files should contain the definition.如果您希望从多个翻译单元访问全局对象,则头文件应仅包含声明(即在当前行前添加extern ),并且 .c 文件中的一个应包含定义。

Secondly, in main() you call an undeclared function processMove .其次,在main()调用一个未声明的函数processMove If you compile in standard mode then the compiler should generate an error message.如果在标准模式下编译,则编译器应生成错误消息。 This is likely the cause of your symptoms, and I would strongly recommend invoking your compiler in modern standard mode with a high diagnostic level, as it would have saved you a lot of time to find out about this problem sooner.这可能是导致您出现症状的原因,我强烈建议您在具有高诊断级别的现代标准模式下调用您的编译器,因为它可以为您节省大量时间来更快地发现此问题。

To fix this, the declaration boardState processMove(boardState, move);为了解决这个问题,声明boardState processMove(boardState, move); should be in declarations.h .应该在declarations.h

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

相关问题 为什么这个双精度类型变量在作为 function 参数传递时默认为 int? - Why is this double type variable default to int when passed as a function parameter? 为什么变量在比较时变为零? - Why variable does change to zero when it is compared? getmaxyx 如何改变传递变量的值? - How does getmaxyx change the value of a passed variable? 更改作为 void* function 参数传递的变量的地址 - Change the address of a variable passed as a void* function parameter 当 function 作为值参数传递给 function 时,如何更改原始变量? - How can a function change the original variable, when it is passed as value argument to the function? 为什么退出此函数后传递给指针参数的指针变量的值没有变化? - Why didn't the value of pointer variable passed to pointer argument change after exiting this function? 为什么函数不会更改变量? - Why function will not change variable? 此函数如何更改传递给它的参数? - How does this function change the parameter passed to it? 为什么将此结构作为函数的参数传递,变化就是值? - Why is this struct passed as argument of a function, change is values? 为什么在运行“插入”函数后我的数字变量会发生变化? - Why does my number variable change after running the 'insert' function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM