I am new to C and have run into this issue twice now.
The problem is that I am getting an access violation error when attempting to run the following program. The exception is thrown in the initialize_board()
function. I put a comment on the specific line.
Any insight from those with more experience would be appreciated!
#include <stdio.h>
#include <stdlib.h>
/* global variables */
const int BOARD_SIZE = 3;
const char X = 'X';
const char O = 'O';
char** active_board;
//creates a square 2d array of size BOARD_SIZE
void create_board() {
//ptrs to array of chars
active_board = (char*)malloc(sizeof(char*)*BOARD_SIZE);
for (int i = 0; i < BOARD_SIZE; i++) {
active_board[i] = (char)malloc(sizeof(char) * BOARD_SIZE);
}
}
//fills board with either char X or O
void initialize_board(char symbol) { //symbol:= X or O
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
active_board[i][j] = symbol;// <---EXCEPTION THROWN HERE
}
}
}
int main() {
create_board();
initialize_board(X);
return 0;
}`