C - 传递struct指针终止程序?如何

时间:2022-02-27 20:30:34

My problem is the initialisePlayer() always makes program crashed when I execute it

我的问题是initialisePlayer()总是让程序在执行时崩溃

1 个解决方案

#1


0  

You should pass player as the address of player to the initialize function:

你应该将玩家作为玩家的地址传递给初始化函数:

    initialisePlayer(&player, &pos, direction);

and allocate memory for it in initialize:

并在初始化中为它分配内存:

void initialisePlayer(Player **player, Position * position, Direction direction);
{
    Player *p= malloc(sizeof(Player));
    p->position.x = position->x;
    p->position.y = position->y;
    p->direction = direction;
    p->moves = 0;
    *player= p;
}

#1


0  

You should pass player as the address of player to the initialize function:

你应该将玩家作为玩家的地址传递给初始化函数:

    initialisePlayer(&player, &pos, direction);

and allocate memory for it in initialize:

并在初始化中为它分配内存:

void initialisePlayer(Player **player, Position * position, Direction direction);
{
    Player *p= malloc(sizeof(Player));
    p->position.x = position->x;
    p->position.y = position->y;
    p->direction = direction;
    p->moves = 0;
    *player= p;
}