[英]Using Structs in C
Hi im building a simple game in c (im new to the language). 您好,我正在用c构建一个简单的游戏(这是该语言的新功能)。 Im using the following structs:
我使用以下结构:
typedef struct
{
int adjacent_mines;
bool revealed;
bool is_mine;
} Tile;
struct GameState
{
Tile tiles[NUM_TILES_X][NUM_TILES_Y];
};
typedef struct GameState GameState;
Im wondering how to properly call and set the structs? 我想知道如何正确调用和设置结构? I have the following code where i would like to set state of each Tile.
我有以下代码,我想在其中设置每个图块的状态。
void intialize_mines(){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
tiles[i,j].revealed = false;
}
}
}
But according to my console output i have done this incorrectly. 但是根据我的控制台输出,我做错了。 How would i go about setting this correctly?
我将如何正确设置此设置?
struct GameState
just declares a type (just as int
is just a type). struct GameState
只是声明一个类型(就像int
只是一个类型一样)。 You have to create a real struct in memory with GameState foo;
您必须使用
GameState foo;
在内存中创建一个真正的结构GameState foo;
similar to a normal variable ( int foo;
). 与普通变量(
int foo;
)类似。 And you can not access the contents of a struct without referencing the struct like foo.tiles
. 而且,如果不引用
foo.tiles
之类的结构,就无法访问该结构的内容。 tiles
on it's own is not known in this scope. 在此范围内不知道
tiles
。
Afterwards you can access the struct with foo.tiles[i][j].revealed
. 之后,您可以使用
foo.tiles[i][j].revealed
访问该结构。
But to have access to this struct in your function you either have to pass it to the function as a pointer or declare the struct in filescope ( also called global). 但是要在您的函数中访问此结构,您必须将其作为指针传递给函数,或者在文件范围(也称为全局)中声明该结构。 I would prefer the first version it is clearer an more function like.
我希望第一个版本的功能更清晰。
Your function would look like this: 您的函数如下所示:
void intialize_mines( GameState *foo){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
foo->tiles[i][j].revealed = false; // -> is a special operator for pointers to structs. It's the same as (*foo).
}
}
}
the corresponding function call would be: 相应的函数调用为:
GameSate bar;
intialize_mines( GameState &bar);
You should also check how to use multidimensional arrays. 您还应该检查如何使用多维数组。 You declared it correctly with two seperate
[]
but in your function you use [x,y]
which is not correct in C. It would be the same as in the declaration tiles[i][j]
您使用两个单独的
[]
正确声明了它,但是在函数中使用了[x,y]
,这在C语言中是不正确的。这与声明tiles[i][j]
For a multi-dimensional array like tiles
, you have to specify array subscript of each dimension within []
like this: 对于像
tiles
这样的多维数组,您必须在[]
指定每个维度的数组下标,如下所示:
tiles[i][j].revealed = false;
This means that revealed
belonging to j
th column of i
th row of tiles
is set to false
. 这意味着
revealed
属于第i
行tiles
第j
列被设置为false
。
And you will have to define a structure variable of the type GameState
before performing any operations on it. 并且,您必须在执行任何操作之前定义
GameState
类型的结构变量。
GameState initGS;
void intialize_mines(){
for (int i =0; i < NUM_TILES_X; i++){
for (int j =0; j < NUM_TILES_Y; j++){
initGS.tiles[i][j].revealed = false;
}
}
}
You just missed to instantiate a GameState structure. 您只是想实例化GameState结构。
GameState gs;
void initialize_mines() {
for (int i =0; i < NUM_TILES_X; i++) {
for (int j =0; j < NUM_TILES_Y; j++) {
gs.tiles[i][j].revealed = false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.