0

I have to initialize a two-dimensional array, but the specific wording is confusing me:

 a game_state variable is created and pointed
 to by the first parameter.  Each element in 
 the board table is created empty

The array has to be initialised in an initialiser function:

void init_game_state(game_state* gp)

And is first created by:

struct game_state_int {
int board[DIMENSION][DIMENSION];
};

With dimension being defined as '4'. Is it as simple as going:

 void init_game_state(game_state* gp)
 {
     *gp = (game_state)malloc(sizeof(struct game_state_int));
      for (int i = 0; i < DIMENSION; i++)
      {
          for (int j = 0; j < DIMENSION; j++)
          {
              (*gp)->board[i][j] = NULL;
        
        }
   }

As I've tried this and have been getting odd results with the rest of the program.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Blither
  • 9
  • 2
  • 2
    `(*gp)->` this is wrong, the `->` already does the dereferencing `*` for you. You should do `gp->` instead. A better way to do this would be to simply call `calloc(1, sizeof(...))` instead of `malloc()`, without the need for the double cycle since `calloc` does exactly what you want: initialize everything to `0`. Also, note that `NULL` should be replaced with `0` here, you are assiging `int`, not pointers. – Marco Bonelli Oct 11 '21 at 07:18
  • The main problem here is that you simply cannot allocate memory for `gp` inside a function with the signature `void init_game_state(game_state* gp)`. See the linked duplicate. There are lots of other problems too but that's the major one. – Lundin Oct 11 '21 at 08:12
  • Your problem may have been incorrectly closed by @MarcoBonelli and Lundin as a duplicate. If the type of `game_state` is `game_state_int *`, then the code shown is not wrong *per se*. However, you need to show more code so that the problem can be diagnosed. If you edit the question to provide a [mre] sufficient to diagnose the problem, and it is not a duplicate of [this question](https://stackoverflow.com/questions/39486797/dynamic-memory-access-only-works-inside-function), then I will reopen it. – Eric Postpischil Oct 11 '21 at 11:30
  • @Lundin: Re “you simply cannot allocate memory for`gp` inside a function”: The code does not allocate memory for `gp` to point to; it allocates memory for `*gp` to point to. – Eric Postpischil Oct 11 '21 at 11:34
  • @EricPostpischil Which is nonsense - the point is that the function declaration as is, doesn't allow any sensible implementation of that function, in case it is supposed to allocate memory for the parameter. Or in case `game_state` is a pointer hidden behind a typedef, then the program has even bigger problems than this. – Lundin Oct 11 '21 at 11:44
  • @Lundin: The function is entirely sensible if `game_state` is a `game_state_int *`—regardless of your opinion about typedef pointers, the code then conforms to the C standard. And your opinion about typedef pointers is not a reason to close the question as a duplicate of a question that it is not a duplicate of. – Eric Postpischil Oct 11 '21 at 11:46
  • @EricPostpischil "Sensible" and "conforms to the C standard" are very different things. And regardless of close reason used, we'd need to know the type of the parameter to answer anyway. – Lundin Oct 11 '21 at 11:54
  • @Lundin: The type of parameter is stated in the assignment specification, “a game_state variable is created and pointed to by the first parameter”, and it is shown in the code, `(game_state *gp)`, and the fact that `game_state` is a pointer to `game_state_int` is consistent with both the allocation of `sizeof(game_state_int)` bytes and the (unnecessary) conversion of the `malloc` result to `game_state`. Basically, all of the code has a clear interpretation and is not incorrect as shown. It was a mistake to close the question as a duplicate. – Eric Postpischil Oct 11 '21 at 12:01
  • @Lundin: The question should have been closed as needs debugging information or needs details, but Stack Overflow does not have a mechanism for changing the close mechanism. As I noted to OP, if they provide the needed information, I will vote to re-open. Correcting your errors is not trolling. You will have better resulst when you correct your errors instead of doubling-down on them. – Eric Postpischil Oct 11 '21 at 13:32

1 Answers1

0

It seems game_state is an alias for the structure game_state_int

typedef struct game_state_int game_state;

If the function accepts a pointer to an object of the structure type then it means that the object already exists and all what you need is to initialize the inner two-dimensional array.

For example

void init_game_state( game_state *gp )
{
    *gp = ( game_state ){ { { 0 } } };
}

And in main you have to write for example

game_state game;

init_game_state( &game );

Another way is to use the standard C function memset instead of the compound literal. For example

#include >string.h>

//...

void init_game_state( game_state *gp )
{
    memset( gp->board, 0, DIMENSION * DIMENSION * sizeof( int ) );
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Re “It seems `game_state` is an alias for the structure `game_state_int`”: There code in the question does not suggest this. If the type of `game_state` is `game_state_int`, then the code in the question is incorrect. If the type of `game_state` is `game_state_int *`, then the code in the question is correct *per se*. – Eric Postpischil Oct 11 '21 at 11:32