0

i have a console based application in which i am using pointer to a pointer. It is minesweeper game in which i need to create board using pointer to a pointer. I have to pass my this pointer to pointer to a function for initialization purpose. But problem is that, it got initialize in function but i am unable to access in main. I tried to use the pass by reference/address. I will be grateful If anyone can help me out in this.

Thanks

    #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);
        }
    }


}
4
  • "I tried to use the pass by reference." In C there is no "pass by reference". C is always and ever "pass by value". Also the code you show does not use any additional indirection. You need to pass to a function the the address of the variable you want to change inside a function. That is &mine_board in your case. Also you need to adjust the function accordingly to take a struct Location***. Commented May 14, 2017 at 12:12
  • by reference i meant passing address of the variable :) Commented May 14, 2017 at 12:16
  • Fair enough, but still, the code you show does not do this. Commented May 14, 2017 at 12:17
  • ok, so now how am i going to initialize it here ? ----------------------------------------------- mine_board = (Location**)malloc(sizeof(Location*) * (game1->height + 2)); for (int i = 0; i < game1->height + 2; i++) { mine_board[i] = (struct Location*)malloc(sizeof(struct Location)* (game1->width + 2)); } Commented May 14, 2017 at 12:20

1 Answer 1

0

You need to pass to a function the address of the variable you want to change inside a function.

That is passing &mine_board in your case. Also you need to adjust the function accordingly to take a what &main_board evaluates to. As main_board is struct Location** that is struct Location*** pmine_board.

Inside the allocating function then use

*pmine_board = malloc( ....

and

(*pmine_board)[i] = malloc( ...

and

(*pmine_board)[i][j].value = ...

Aside from this

  • just drop all those useless casts
  • add error checking (and handling) to those functions that may fail (e.g. malloc())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.