简体   繁体   中英

C Program doesn't continue to follow code in main() function after calling a function inside of it

Won't execute the following code in the main() function. Any input is appreciated. PS I was in the beginning stages of creating a tic-tac-toe-game.

void printBoard(int slotNumber);

int main()
{
int slotNumberMain = -1;
printf("prints this\n");
printBoard(slotNumberMain);
printf("but not this\n");


return 0;
}

void printBoard(int slotNumber)
{

int boardSlots[3][3];
int i;
int j = 0;

for (i = 1; i <= 9; i++){
    char box[4] = {'[', ' ', ']', '\0'};
    boardSlots[j][i - 1] = i;

    if (slotNumber == i){
        char box[4] = {'[', 'X', ']', '\0'};
    }

    printf("%s", box);
    if (i % 3 == 0){
        j++;
        printf("\n");
        }
    }

}

Edit: The reason there is a slotNumber variable is because originally I had added a enterNumber() function (which would be called at the end of printBoard() and have had you enter a number and mark the box with an 'x' after recalling printBoard(slotNumberMain)) however I haven't added the code here, because I realized that the problem was strictly within printBoard() and main.)

Edit 2: I have solved the issue, it was due to the boardSlots array being overrun like someone had suggested.

Thanks to everyone who attempted to help.

I have solved the issue, it was due to the boardSlots array being overrun like someone had suggested.

void printBoard(int slotNumber)
{

    int boardSlots[3][3];

    int i;
    int j = 1;
    char box[4] = {'[', ' ', ']', '\0'};

    for (i = 1; i <= 3 && j <= 3; i++){

        boardSlots[j - 1][i - 1] = i * j;

        if (slotNumber == i){
            char box[4] = {'[', 'X', ']', '\0'};
        }

        printf("%s", box);
        if (i == 3){
            j++;
            i = 0;
            printf("\n");
        }
    }

}

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