C - 传递结构指针会终止程序?如何

标签 c pointers struct

我的问题是当我执行它时initialisePlayer()总是使程序崩溃

最佳答案

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

    initialisePlayer(&player, &pos, direction);

并在初始化时为其分配内存:

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

关于C - 传递结构指针会终止程序?如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36478712/

相关文章:

c++ - 使用指针对数组进行动态内存分配的好处

c - 填充结构 C 时出现段错误,缩小为一行

c++ - 基于字符串对结构数组进行排序

c - 使用 malloc() 和 free() 函数时出错

c - 在 C 中存储大数的数据类型

c++ - 交叉构建,链接问题

c++ - 如何调试非确定性内存损坏?

c - 需要更多这方面的信息!中止(核心转储)

c - 如何用斐波那契数列动态填充数组

matlab - 将结构的字段加载到变量中 (MATLAB)