Home > Mobile >  How is a pointer to an array of int pointers initialized dynamically using malloc?
How is a pointer to an array of int pointers initialized dynamically using malloc?

Time:08-31

I'm currently working on a Tic Tac Toe game in C and have run into a bit of a snag. I've defined a struct to represent the game board (see below, "board.h") and am trying to figure out to define a sort of "constructor" function that will initialize a default version of a Board struct dynamically and return a pointer to it. More specifically, I'm having difficulty determining exactly how I should use the malloc function to dynamically allocate the space for the prows field of the Board struct, regarding both the input to the malloc function and how to cast the result of the malloc function from type "void pointer" - void * - to type "pointer to array of int pointers."

In the code box below, I've included the contents of both my "board.h" and "board.c" files: board.h contains my definition of the Board struct and the prototypes for the functions implemented in board.c . In board.c, I've already tried implementing the initBoard function to initialize a Board struct on heap and return a pointer to it, however I'm unsure if my handling of the initialization of the prows field is correct (I've included my step by step reasoning in the initBoard function implementation). I was hoping someone could help me understand how to use malloc to initialize the prows field and why it must be done that way.

/***
 * =======================================
 *                board.h
 * =======================================
***/

#ifndef BOARD_H_INCLUDED
#define BOARD_H_INCLUDED

const unsigned int boardDim = 3;

typedef struct {
    int * (*prows)[boardDim];
    int score;
    unsigned int turn;
    _Bool isTerminal;
} Board;

Board * initBoard(void);

#endif



/***
 * =======================================
 *                board.c
 * =======================================
***/

#include <stdio.h>
#include <stdlib.h>

#include "board.h"

/*
 * to initialize the prows field of a new Board struct, I figured it would be a good
 * approach to first initialize the array of int pointers to which prows points to and 
 * then initialize prows by taking the address of the initialized array of int pointers
*/
Board * initBoard(void) {
    /*
     * initialize array of int pointers called "rows" ...
     *
     * from what I know, rows stores the address of the first element in the array's
     * memory block, so a pointer to the "rows" array will essentially be a pointer to an
     * int pointer, so we should cast the returned void pointer (void *) from malloc to 
     * type pointer to int pointer (int **) ...
     * 
     * lastly, because rows will contain (boardDim) number of pointers (one for each row
     * in the board), inside malloc, I should pass "boardDim * sizeof(int *)" to allocate
     * (boardDim) number of int pointers
    */
    int * rows[boardDim] = (int **) malloc(boardDim * sizeof(int *));
    
    int * row;
    for (row = rows; row < rows   boardDim; row  ) {
        row = (int *) malloc(sizeof(int));
    }

    int * (*prows)[boardDim] = &rows;

    int score = 0, turn = 0;
    _Bool isTerminal = 0;

    Board * board = (Board *) malloc(sizeof(Board));

    board->prows = prows;
    board->score = score;
    board->turn = turn;
    board->isTerminal = isTerminal;

    return board;
}

CodePudding user response:

Your program is overly complicated - you should take some time to rethink your approach and plan everything out.

Nevertheless here is how to allocate what you desire:

#define boardDim 3

int *(*prows)[boardDim];
prows = malloc(sizeof(int *[boardDim]));
  • Related