初始化指向结构指针的指针
Initialise Pointer to Pointer for struct
我有一个基于控制台的应用程序,我在其中使用指向指针的指针。这是扫雷游戏,我需要使用指向指针的指针来创建棋盘。我必须将我的 this 指针传递给指向函数的指针以进行初始化。但问题是,它在函数中进行了初始化,但我无法在 main 中访问。我尝试使用 reference/address 的通行证。如果有人能帮助我,我将不胜感激。
谢谢
#include <stdlib.h>
#include<stdio.h>
#define MINE 0
#define FLAG 1
#define REVEALED 2
struct Location {
int value;
char status_byte;
};
struct Game {
int width;
int height;
int mines;
int mines_flagged;
int flags;
};
int mine_clicked = 1;
int board_width;
int board_height;
int total_mines;
void initialize_mine_values(struct Game *game, struct Location** mine_board);
void main() {
struct Game game;
struct Location** mine_board = NULL;
//Location actualThing;
//Location *pointer = &actualThing;
// Location **mine_board = &pointer;
printf("\t** Student_number Student_name Assignment 3** \n");
initialize_mine_values(&game, &(*mine_board));
printf("game.width : %d", game.width);
//test//
/*for (int i = 0; i < game.width + 2; i++)
{
for (int j = 0; j < game.height + 2; j++)
{
mine_board[i][j].value = 12;
}
}
*/
for (int i = 0; i < game.width + 2; i++)
{
for (int j = 0; j < game.height + 2; j++)
{
printf("\n %d %d", i, j);
// Location l = mine_board[i][j];
printf(" , here it is location value %d", mine_board[i][j].value);
}
}
//test end
scanf("%d", &board_height);
getchar();
}
void initialize_mine_values(struct Game *game1, struct Location** mine_board)
{
//mines_flagged and flag is set to 0
game1->flags = 0;
game1->mines_flagged = 0;
//take width of the game board
printf("\nEnter the width of the board");
scanf("%d", &(game1->width));
printf("\nEnter the height of the board");
scanf("%d", &(game1->height));
do {
printf("Enter total number of mines for the game, Number of mines should be less than %d ", game1->width*game1->height);
scanf("%d", &total_mines);
} while (total_mines >= game1->width*game1->height);
//initializing mine board
mine_board = (Location**)malloc(sizeof(Location*) * (game1->height + 2));
// mine_board[0] = (struct Location*)malloc(sizeof(struct Location) * (game1->width + 2) * (game1->height + 2));
for (int i = 0; i < game1->height + 2; i++) {
mine_board[i] = (struct Location*)malloc(sizeof(struct Location)* (game1->width + 2));
}
for (int i = 0; i < game1->width + 2; i++)
{
for (int j = 0; j < game1->height + 2; j++)
{
mine_board[i][j].value = i+j;
}
}
for (int i = 0; i < game1->width + 2; i++)
{
for (int j = 0; j < game1->height + 2; j++)
{
printf("\n %d %d", i, j);
// Location l = mine_board[i][j];
printf(" , here it is location value %d", mine_board[i][j].value);
}
}
}
您需要将要在函数内更改的变量的地址传递给函数。
在你的情况下,这就是通过 &mine_board
。您还需要相应地调整函数以获取 &main_board
的评估结果。因为 main_board
是 struct Location**
即 struct Location*** pmine_board
.
在分配函数中然后使用
*pmine_board = malloc( ....
和
(*pmine_board)[i] = malloc( ...
和
(*pmine_board)[i][j].value = ...
除此之外
- 扔掉那些没用的演员表
- 为那些可能失败的函数添加错误检查(和处理)(例如
malloc()
)
我有一个基于控制台的应用程序,我在其中使用指向指针的指针。这是扫雷游戏,我需要使用指向指针的指针来创建棋盘。我必须将我的 this 指针传递给指向函数的指针以进行初始化。但问题是,它在函数中进行了初始化,但我无法在 main 中访问。我尝试使用 reference/address 的通行证。如果有人能帮助我,我将不胜感激。
谢谢
#include <stdlib.h>
#include<stdio.h>
#define MINE 0
#define FLAG 1
#define REVEALED 2
struct Location {
int value;
char status_byte;
};
struct Game {
int width;
int height;
int mines;
int mines_flagged;
int flags;
};
int mine_clicked = 1;
int board_width;
int board_height;
int total_mines;
void initialize_mine_values(struct Game *game, struct Location** mine_board);
void main() {
struct Game game;
struct Location** mine_board = NULL;
//Location actualThing;
//Location *pointer = &actualThing;
// Location **mine_board = &pointer;
printf("\t** Student_number Student_name Assignment 3** \n");
initialize_mine_values(&game, &(*mine_board));
printf("game.width : %d", game.width);
//test//
/*for (int i = 0; i < game.width + 2; i++)
{
for (int j = 0; j < game.height + 2; j++)
{
mine_board[i][j].value = 12;
}
}
*/
for (int i = 0; i < game.width + 2; i++)
{
for (int j = 0; j < game.height + 2; j++)
{
printf("\n %d %d", i, j);
// Location l = mine_board[i][j];
printf(" , here it is location value %d", mine_board[i][j].value);
}
}
//test end
scanf("%d", &board_height);
getchar();
}
void initialize_mine_values(struct Game *game1, struct Location** mine_board)
{
//mines_flagged and flag is set to 0
game1->flags = 0;
game1->mines_flagged = 0;
//take width of the game board
printf("\nEnter the width of the board");
scanf("%d", &(game1->width));
printf("\nEnter the height of the board");
scanf("%d", &(game1->height));
do {
printf("Enter total number of mines for the game, Number of mines should be less than %d ", game1->width*game1->height);
scanf("%d", &total_mines);
} while (total_mines >= game1->width*game1->height);
//initializing mine board
mine_board = (Location**)malloc(sizeof(Location*) * (game1->height + 2));
// mine_board[0] = (struct Location*)malloc(sizeof(struct Location) * (game1->width + 2) * (game1->height + 2));
for (int i = 0; i < game1->height + 2; i++) {
mine_board[i] = (struct Location*)malloc(sizeof(struct Location)* (game1->width + 2));
}
for (int i = 0; i < game1->width + 2; i++)
{
for (int j = 0; j < game1->height + 2; j++)
{
mine_board[i][j].value = i+j;
}
}
for (int i = 0; i < game1->width + 2; i++)
{
for (int j = 0; j < game1->height + 2; j++)
{
printf("\n %d %d", i, j);
// Location l = mine_board[i][j];
printf(" , here it is location value %d", mine_board[i][j].value);
}
}
}
您需要将要在函数内更改的变量的地址传递给函数。
在你的情况下,这就是通过 &mine_board
。您还需要相应地调整函数以获取 &main_board
的评估结果。因为 main_board
是 struct Location**
即 struct Location*** pmine_board
.
在分配函数中然后使用
*pmine_board = malloc( ....
和
(*pmine_board)[i] = malloc( ...
和
(*pmine_board)[i][j].value = ...
除此之外
- 扔掉那些没用的演员表
- 为那些可能失败的函数添加错误检查(和处理)(例如
malloc()
)