0

I have been asked to finish some code someone else started, and I am completely confused on how to copy a U32 value inside an struct. These are the relevant parts of the various structs; note that I am trimming a lot because those are some seriously huge structs:

typedef struct AttackerList {
    U32 count;
} AttackerList;

typedef struct AggroVars {
    AttackerList attackerList;
}

typedef struct Player {
    U32 aiAttackers;
}

Now, in the function I am trying to modify:

void attackTarget(Player* target) {
   AggroVars* aiTarget;
   // Tons of code here.
   aiTarget->attackerList.count++;
   target->aiAttackers = aiTarget->attackerList.count;
   // Tons more code here.
}

That last line is the one that is causing me all sorts of grief. It does work, I can see in the debug output how many critters are attacking the player; but it causes a crash whenever the AI loses interest in the target. I know this has something to do with pointers, but sprinkling asterisks on the code results in either "invalid indirection" or "differs in levels of indirection". I am pretty much stumped on how to retrieve just the value of aiTarget->attackerList.count without any weird pointer stuff.

4

2 回答 2

0

我建议不要在初始化或修改这些变量后在我们看不到的大量代码中添加断言,而不是“撒上星号”:

#include <assert.h>

...
assert (target != NULL);
assert (aiTarget != NULL);

这可能会为您指明正确的方向。

于 2014-01-10T12:02:42.613 回答
0

您首先需要为每个结构分配内存,因此在您的代码中进行以下更改

void attackTarget(Player* target) 
{
   AggroVars* aiTarget = malloc(sizeof(AggroVars));

   aiTarget->attackerList.count++;

   target->aiAttackers = aiTarget->attackerList.count;

}
于 2014-01-10T12:15:12.470 回答