Home > Software engineering >  How do I access a variable in a struct within a 2d array inside of another struct?
How do I access a variable in a struct within a 2d array inside of another struct?

Time:03-18

I am working on an assignment for my class and I don't understand how I am supposed to access the variable inside a game_piece struct. I need to check to see if it is the default assignment or not, but am confused about how to do so.

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

struct game_piece
{
    char label[30];
};

struct game_board
{
    struct game_piece **board;
};

void game_piece_init_default(struct game_piece *piece)
// This function initializes a game piece with the default label.
{
    strcpy(*piece->label, "SSS");
}

void game_piece_init(struct game_piece *piece, char *new_label)
// This function initializes a game piece with a specified label.
{
    strcpy(*piece->label, new_label);
}

char *game_piece_get_label(struct game_piece *piece)
// This function returns the label of a game piece.
{
    return *piece->label;
}

int game_board_add_piece(struct game_board *game_board, struct game_piece *piece, int row, int col)
{
    if (game_board_is_space_valid(game_board, row, col))
    {
        if (strcmp((game_board->board[row][col])->label, "SSS")) // Error here
        {
            return 1;
        }
    }
    else
        return 0;
}

CodePudding user response:

Seems you are missing the understanding how to work with pointers (nothing to shame on as you are probably a beginner if this is a class assignment). The -> is dereferencing, but your ´game_board´ apparently does not have pointers to instances of ´game_piece´, but the content directly. Because of that, you have to change it to a ´.´.

Also, as both ´->´ and the prefix ´´ are dereferencing, this will not work. I recommend to remove the ´´s at the specific points.

Beyond your question:

  • ´strcpy´ would create a buffer overflow, if your new_label at initialization is longer than 30 characters, including the terminating \0. I recommend to switch to strncpy to limit this to the size of the target array, but also have some error handling for the case the caller provides a too long string.
  • Your nested if does not always have a defined return value, which is even mourned by gcc in default settings.
  • Instead of using int, modern C has a bool datatype, which is not really type safe, but better than nothing.

As you did not provide a main, to have a minimally running example except your issue, I was not able to test all this. The code I had at the end was:

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

struct game_piece
{
    char label[30];
};

struct game_board
{
    struct game_piece **board;
};

void game_piece_init_default(struct game_piece *piece)
// This function initializes a game piece with the default label.
{
    strcpy(piece->label, "SSS");
}

void game_piece_init(struct game_piece *piece, char *new_label)
// This function initializes a game piece with a specified label.
{
    strcpy(piece->label, new_label);
}

char *game_piece_get_label(struct game_piece *piece)
// This function returns the label of a game piece.
{
    return piece->label;
}

int game_board_is_space_valid(struct game_board *game_board, int row, int col)
{
  return 1;
}

int game_board_add_piece(struct game_board *game_board, struct game_piece *piece, int row, int col)
{
    if (game_board_is_space_valid(game_board, row, col))
    {
        if (strcmp((game_board->board[row][col]).label, "SSS"))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
        return 0;
}

CodePudding user response:

I think the type of game_piece[][] is game_piece * ,but the type of game_board is game_piece ** .

  • Related