[英]Creating an object / typedef struct using dynamic arrays in C
我试图从多个dyanimc数组中用C创建一个对象(typedef结构),但是在给成员赋值时遇到一些问题,我的代码如下:
#define MAX_SHIPS 200
typedef struct enemy {
int enemyX[MAX_SHIPS];
int enemyY[MAX_SHIPS];
int enemyDistance[MAX_SHIPS];
int enemyHealth[MAX_SHIPS];
int enemyType[MAX_SHIPS];
}enemy;
^定义MAX_SHIPS并创建结构敌人。
number_of_friends = 0;
number_of_enemies = 0;
if (number_of_ships > 1)
{
for (i=1; i<number_of_ships; i++)
{
if (IsaFriend(i))
{
friendX[number_of_friends] = shipX[i];
friendY[number_of_friends] = shipY[i];
friendHealth[number_of_friends] = shipHealth[i];
friendFlag[number_of_friends] = shipFlag[i];
friendDistance[number_of_friends] = shipDistance[i];
friendType[number_of_friends] = shipType[i];
number_of_friends++;
}
else
{
int x;
for (x = 0; x < number_of_ships; x++)
{
enemy[x].enemyX = shipX[i];
enemy[x]. enemyY = shipY[i];
enemy[x].enemyDistance = shipDistance[i];
enemy[x].enemyHealth = shipHealth[i];
enemy[x].enemyType = shipType[i];
}
目前,我收到错误int x expected an identifier
。
enemyX[number_of_enemies] = shipX[i];
enemyY[number_of_enemies] = shipY[i];
enemyHealth[number_of_enemies] = shipHealth[i];
enemyFlag[number_of_enemies] = shipFlag[i];
enemyDistance[number_of_enemies] = shipDistance[i];
enemyType[number_of_enemies] = shipType[i];
number_of_enemies++;
}
}
}
^代码我想删除/替换为敌人struct的创建。
作为船舶矩阵的结构很笨拙且浪费。 您经常为了使用单独的船只而迷惑阵列。 您不必分配很大的内存即可容纳最大数量的船只。 您需要为敌人和友善者复制整个结构。 您只需要在一艘船上将所有内容复制进出结构即可...
而是制作一个单独的Ship
结构。 然后,您可以只传递指针,并对友军和敌舰使用相同的结构。 您可以将友好和敌方船只保留在“ Ship
指针列表中,而无需复制所有数据。
#include <stdio.h>
#include <stdlib.h>
/* A structure to store ships */
typedef struct {
int x;
int y;
int distance;
int health;
int type;
} Ship;
/* No matter how simple the struct, always write functions to
create and destroy it. This makes using it simpler, and it
shields your code from making future changes to the struct. */
Ship *Ship_new() {
/* Use calloc(), rather than malloc(), to guarantee everything
is initialized to 0 rather than dealing with garbage. */
return calloc(1, sizeof(Ship));
}
void Ship_destroy( Ship *ship ) {
free(ship);
}
/* Constants are easier to debug than macros */
const int MAX_FRIENDLIES = 200;
const int MAX_ENEMIES = 200;
int main() {
/* Store just a list of pointers to Ships. This is more
flexible and saves a lot of memory. */
Ship *friendlies[MAX_FRIENDLIES];
Ship *enemies[MAX_ENEMIES];
/* Make new ships for demonstration purposes */
Ship *enemy = Ship_new();
Ship *friendly = Ship_new();
/* Just to demonstrate setting values */
enemy->x = 5;
enemy->y = 10;
enemy->health = 100;
enemy->type = 5;
friendly->x = 99;
friendly->y = 23;
friendly->health = 50;
friendly->type = 10;
/* Assign them to their lists. Since it's a list of Ship *
we only need to copy the pointer, not all the data. */
friendlies[0] = friendly;
enemies[0] = enemy;
/* Make use of them. friendlies[0] and friendly point to the
same ship, not a copy. */
printf("Friendly #1 health: %d\n", friendlies[0]->health);
printf("Enemy #1 health: %d\n", enemies[0]->health);
}
该代码应为
else
{
enemy.enemyX[number_of_enemies] = shipX[i];
enemy.enemyY[number_of_enemies] = shipY[i];
enemy.enemyDistance[number_of_enemies] = shipDistance[i];
enemy.enemyHealth[number_of_enemies] = shipHealth[i];
enemy.enemyType[number_of_enemies] = shipType[i];
number_of_enemies++;
}
注意,方括号现在位于struct
成员的末尾。
如@Schwern所说,对于延迟的回复/响应感到抱歉,该结构除其他外还显得很尴尬。 最后,我发现最简单的方法是为每个属性编写单独的较小函数。
但是再次,非常感谢你们的回答:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.