简体   繁体   中英

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. It passes one of the boardstate types and one of the move types:

#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:

#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. 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. 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; . Remember that #include is a plain text replacement, so both of your c files end up with a definition of that object. 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.

Secondly, in main() you call an undeclared function 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); should be in declarations.h .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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