将数组的指针传递给函数

时间:2017-04-01 05:32:30

标签: c arrays pointers struct

我目前正在编写代码,该代码采用不同的char和num数组(代表战舰的船)并将其应用到函数中。在下面的主要代码中,我执行我想要做的事情,但是当我尝试将数组diff_ships放入函数random_place_ships_on_board时,我收到一条错误消息。任何帮助都会非常有帮助。

这是我的头文件

typedef struct game_board
{
    int board[10][10];
    int row;
    int col;
    char symbol;
}Game_Board;

typedef struct ships
{
    int legnth;
    char symbol;
}Ships;

int randomlly_place_ships_on_board(Ships *diff_ships[], Game_Board *player);

这是主

中的代码
Game_Board person, computer;
Ships diff_ships[5] = { {5, 'c'}, {4, 'b'}, {3, 'r'}, {3, 's'}, {2, 'd'} };

person.symbol = '~';
person.row = 10;
person.col = 10;
computer.symbol = '-';
computer.row = 10;
computer.col = 10;


printf("Player 1\n");
initalize_game_board(&person);
printf("\nPlayer 2\n");
initalize_game_board(&computer);

randomlly_place_ships_on_board(&diff_ships[5], &person);

以下是main中完美运行的代码

printf("%c\n", diff_ships[0].symbol);
printf("%c\n", person.board[0][0]);
person.board[0][0] = diff_ships[0].symbol;
printf("%c\n", person.board[0][0]);

这是我试图在

中运行代码的函数
int randomlly_place_ships_on_board(Ships *diff_ships[], Game_Board *player)
{
    int direction = 0, i = 0, cell_row = 0, cell_col = 0;

    cell_row = rand() % 6;
    cell_col = rand() % 10;

    printf("Placing ship\n");

                 //this call to an array causes an error
    printf("%c", diff_ships[0]->symbol);

     //this is the line also causing the error
    player->board[0][5] = diff_ships[0]->symbol;
}

0 个答案:

没有答案