【发布时间】:2017-08-30 09:04:50
【问题描述】:
基本上,我要做的是更改函数内的结构变量。代码如下:
int weapon_equip(struct player inventory, int in) {
int x = in - 1, y;
int previous_wep[4];
//Stores the values of the previous equipped weapon.
for(y = 0; y < 4; y++)
previous_wep[y] = inventory.weapons[0][y];
/* Since the equipped weapon has a first value of 0,
I check if the player hasn't chosen a non-existant
item, or that he tries to equip the weapon again.*/
if(inventory.weapons[x][TYPE] != NULL && x > 0) {
inventory.weapons[0][TYPE] = inventory.weapons[x][TYPE];
inventory.weapons[0][MATERIAL] = inventory.weapons[x][MATERIAL];
inventory.weapons[0][ITEM] = inventory.weapons[x][ITEM];
inventory.weapons[0][VALUE] = inventory.weapons[x][VALUE];
inventory.weapons[x][TYPE] = previous_wep[TYPE];
inventory.weapons[x][MATERIAL] = previous_wep[MATERIAL];
inventory.weapons[x][ITEM] = previous_wep[ITEM];
inventory.weapons[x][VALUE] = previous_wep[VALUE];
}
}
基本上,该函数的作用是将所选武器阵列的第一个值更改为0,使其装备给玩家。它会交换已装备武器的位置,与要装备的选定武器。
但问题是 - 我必须更改函数中的很多变量,并且它们都属于一个结构。我知道如何更改函数中的普通整数(使用指针),但我不知道如何使用结构变量来更改。
【问题讨论】:
-
嗯,指向结构变量的指针……应该这样做。
-
struct player inventory-->struct player *inventory,inventory.weapons[0][TYPE] = ...-->inventory->weapons[0][TYPE] = ....在调用方struct player inventory;..weapon_equip(&inventory, ... -
另外,你应该返回值。
标签: c parameter-passing function-call