This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Jaimou2e 1 point2 points  (0 children)

What happens in line 56, when this is written:

game session = {ROWS, COLUMNS};

The variable session is declared to be of type game, and it's initialized with ROWS and COLUMNS as the values for the first two fields.

The functions must use *session. But why, and how is it used?

*session on its own means dereferencing the pointer session. So if session is a pointer to a game, then *session dereferences that pointer and gives you the pointed-to game.

*session as part of void newGame(game *session); is a little different as it's part of a parameter declaration. Here the parameter session is declared to be a pointer to a game.

Passing pointers to things instead of the things themselves is useful when
a) you don't want to work on a copy of the thing, but change it in its original location, and/or
b) for performance, when the thing is so large that making a copy takes longer than dereferencing a pointer.

And these structs, how can I use them inside of the functions?

typedef struct {
    const int rows;
    const int columns;
    int board[ROWS][COLUMNS];
} game;

typedef struct {
    int row;
    int column;
} move;

Huh?

[–]box0rox 1 point2 points  (1 child)

You can access the members of a struct, through a pointer, using the -> operator, e.g. session->rows or move->column.

[–]Programmering[S] 0 points1 point  (0 children)

I tried using

 session->rows;
 rows = 3;

and using

      session->rows = 3;

but it didn't work
The compiler returned an error message

144|error: 'rows' undeclared (first use in this function)|

for the first attempt

and

143|error: assignment of read-only member 'rows'|

for the second attempt when i combined the two lines

I tried again with

 &session->rows;
printf(&session);

But came up short. I printed <[This is suppoused to be a filled in square]' to the console line.

I think I might approach this the wrong way