[英]How do I access a variable in a struct within a 2d array inside of another struct?
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.我正在为我的 class 分配作业,但我不明白我应该如何访问 game_piece 结构中的变量。 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;
}
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).似乎您不了解如何使用指针(没有什么可耻的,因为如果这是 class 作业,您可能是初学者)。 The
->
is dereferencing, but your game_board
apparently does not have pointers to instances of game_piece
, but the content directly. ->
正在取消引用,但您的game_board
显然没有指向game_piece
实例的指针,而是直接指向内容。 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
.new_label
超过 30 个字符(包括终止符\0
), strcpy
会造成缓冲区溢出。 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.strncpy
以将其限制为目标数组的大小,但对于调用者提供的字符串太长的情况也有一些错误处理。if
does not always have a defined return
value, which is even mourned by gcc
in default settings.if
并不总是具有定义的return
值,在默认设置中甚至被gcc
哀悼。int
, modern C has a bool
datatype, which is not really type safe, but better than nothing.int
,现代 C 有一个bool
数据类型,这不是真正的类型安全,但总比没有好。 As you did not provide a main
, to have a minimally running example except your issue, I was not able to test all this.由于您没有提供
main
,除了您的问题之外还有一个运行最少的示例,因此我无法测试所有这些。 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;
}
I think the type of game_piece[][] is game_piece *, but the type of game_board is game_piece **.我认为game_piece[][]的类型是game_piece *,而game_board的类型是game_piece **。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.