I am writing a simple board game. What I want to do when the program runs first print the empty board, then asks the user for X and Y coordinates and then prints the board with the player token in the coordinates he entered. When I run the program it prints the empty board and then asks me for the X coordinate and then the Y coordinate, then it gives me the following error:
X: 1, Y: 2
Y: 2, Y: 3
Segmentation fault (core dumped)
What is making that error and how do I fix it? Thank You
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
struct p1Data{
int xCoordinate;
int yCoordinate;
bool flag;
};
char **gameBoard;
int height, width, boardArea, xCor, yCor, dataSize, i;
char **allocateMemory(int boardHeight, int boardWidth);
void fillBoard(char **board, int height, int width, struct p1Data data[], int sizeData);
void printBoard(char **board, int boardHeight, int boardWidth);
void freeBoardArray(char **board, int boardHeight);
int main(int argc, char** argv ){
height = 4;
width = 4;
boardArea = height * width;
struct p1Data data[boardArea];
dataSize = sizeof(data)/sizeof(data[0]);
data[0].flag = false;
gameBoard = allocateMemory(height, width);
fillBoard(gameBoard, height, width, data, dataSize);
printBoard(gameBoard, height, width);
printf("\n");
for(i = 0; i < boardArea; i++ ){
printf("Enter X-Coordinate: ");
scanf("%d", &xCor);
printf("Enter Y-Coordinate: ");
scanf("%d", &yCor);
data[i].flag = true;
data[i].xCoordinate = xCor;
data[i].yCoordinate = yCor;
fillBoard(gameBoard, height, width, data, dataSize);
printBoard(gameBoard, height, width);
printf("\n");
}
return 0;
}
char **allocateMemory(int boardHeight, int boardWidth){
int i;
char **gameBoard;
gameBoard = (char **)malloc(sizeof(char *)*boardHeight);
for( i = 0; i < boardWidth; i++ ){
gameBoard[i] = (char *)malloc(sizeof(char)*boardWidth);
}
return gameBoard;
}
void fillBoard(char** board, int height, int width, struct p1Data data[], int sizeData){
int i, j, x, y;
for(i = 0; i < height; i++){
for(j = 0; j < width; j++ ){
board[i][j] = '.';
}
}
if(data[0].flag == true ){
for( i = 0; i < sizeData; i++ ){
x = data[i].xCoordinate;
y = data[i].yCoordinate;
board[x][y] = 'O';
printf("X: %d, Y: %d\n", x, y);
}
}
}
void printBoard(char **board, int boardHeight, int boardWidth){
int i, j;
printf("/");
for( i = 0; i < boardWidth; i++ ){
printf("-");
}
printf("\\");
printf("\n");
for(i = 0; i < boardHeight; i++ ){
printf("|");
for(j = 0; j < boardWidth; j++ ){
printf("%c", board[i][j]);
}
printf("|");
printf("\n");
}
printf("\\");
for( i = 0; i < boardWidth; i++ ){
printf("-");
}
printf("/");
}
void freeBoardArray(char **board, int boardHeight){
int i;
for( i = 0; i < boardHeight; i++ )
free(board[i]);
free(board);
}
if (data[0].flag == true) {
for (i = 0; i < sizeData; i++) {
x = data[i].xCoordinate; //<== not intialized
y = data[i].yCoordinate; //<== not intialized
board[x][y] = 'O'; //<= x/y are undefined
printf("X: %d, Y: %d\n", x, y);
}
}
You have to initialize data[i].xCoordinate
and data[i].yCoordinate
otherwise these values are undefined. Later you call board[x][y]
which causes the error.
//initialize here:
for (i = 0; i < boardArea; i++)
{
data[i].flag = false;
data[i].xCoordinate = 0;
data[i].yCoordinate = 0;
}
fillBoard(gameBoard, height, width, data, dataSize);
...