2021年4月5日星期一

Implementing code for the following functions in gameBoard

gameBoard *InitBoard(char *space, int nos,void (*toStringGameboard)(void *S), int (* getNumberOfSpaces) (void * S),char * (* getTypeOfSpace)(void * S))      {          gameBoard *gb= (gameBoard *) malloc(sizeof(gameBoard));          strcpy(gb->typeOfSpace, space);          //implement the function pointers          return gb;      }        char * getTypeOfSpace(void  *board) {          return ???;      }            int getNumberOfSpaces(void * board) {          return ???;      }        void toStringGameboard(void * S){      //print out something informative and cute          return;      }  

I am very confused on void pointers. So far, for the first method, this is what I have. Essentially, since I do not understand what is going on, I've just repeated the parameters of the function in an attempt to initialize them. Is this correct, if not where do I go from here?

gameBoard *InitBoard(char *space, int nos,void (*toStringGameboard)(void *S),             int (* getNumberOfSpaces) (void * S),char * (* getTypeOfSpace)(void * S))  {      gameBoard *gb= (gameBoard *) malloc(sizeof(gameBoard));      strcpy(gb->typeOfSpace, space);      void(*toStringGameboard)(void *S);      int(*NumberOfSpaces)(void *S);      char(*getTypeOfSpace)(void *S);      return gb;  }  

For the second method, I'm thinking I can use char typeOfSpace[20], as written into gameBoard.h, but am not entirely sure how to go about that:

typedef struct gameBoard {      char typeOfSpace[20];   //"WIN", "BACK", "FORWARD", "SAFE"      int numberOfSpaces; //if the type of space is "BACK", "FORWARD", then this is the number of spaces to move          int (* getNumberOfSpaces) (void * S);      void (* toStringGameboard)(void * S);      char * (* getTypeOfSpace)(void * S);  }gameBoard;    gameBoard *InitBoard(char *typeOfSpace, int nos,                      void (*toStringGameboard)(void *S),                       int (* getNumberOfSpaces) (void * S),                      char * (* getTypeOfSpace)(void * S));    char * getTypeOfSpace(void  *board);  int getNumberOfSpaces(void *board);  void toStringGameboard(void *board);  

No clue on how to do the second two, any help would be excellent!

https://stackoverflow.com/questions/66960786/implementing-code-for-the-following-functions-in-gameboard April 06, 2021 at 07:01AM

没有评论:

发表评论