C传递包含结构数组的结构

时间:2014-12-05 21:17:42

标签: c arrays struct

我正在尝试一个包含在另一个Struct内部的结构数组 EG(Struct1.StructArray [])

代码如下所示:

struct bullet{
    int x;
    int y;
    int exist;
    int type;
};

struct tank{
    int x;
    int alive;
    int shotsfired;
    struct bullet shots[50];
};

我试图传递正好射击[] Struct作为指向以下功能的指针。

int get_alien_collision(struct bullet *bulletStruct)

我用来传递结构的代码行如下。

a = get_alien_collision(&player.shots[i])

然而,我无法访问函数内的shot [i]中的任何数据(我通过尝试将" bulletStruct-> x"的值打印到第一个屏幕来确认20,即使它在main()中的结构打印正常,它们都是0

Main.ccalculations.c我的完整代码(在pastebin上)它令人难以置信的混乱,可能充满了许多不良做法,因为这是我第一次编码(我相信是C)< / p>

2 个答案:

答案 0 :(得分:0)

如果你想要更明确,你可以使用parens,或者你可以通过定义中间体来确保你传递正确的东西:

struct bullet currentBullet=player.shots[i];
struct bullet *bp=&currentBullet;
get_alien_collision(bp);

答案 1 :(得分:0)

你传递结构看起来很好但是在你的函数中你分配给bulletStruct而不是测试等价。

if ((bulletStruct->x = alienPPositionx + x)&&(bulletStruct->y = alienPPositiony + y))

应该阅读

if ((bulletStruct->x == alienPPositionx + x)&&(bulletStruct->y == alienPPositiony + y))

解决这个问题的一种方法是使用const参数声明函数

void function ( struct bullet const* bulletStruct)...