在C中使用动态数组创建对象/类型定义结构

在C中使用动态数组创建对象/类型定义结构,c,arrays,struct,C,Arrays,Struct,我正在尝试从多个dyanimc数组在C中创建一个对象(typedef struct),但在为成员赋值时遇到了一些问题,我的代码如下: #define MAX_SHIPS 200 typedef struct enemy { int enemyX[MAX_SHIPS]; int enemyY[MAX_SHIPS]; int enemyDistance[MAX_SHIPS]; int enemyHealth

我正在尝试从多个dyanimc数组在C中创建一个对象(typedef struct),但在为成员赋值时遇到了一些问题,我的代码如下:

    #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_飞船并创建结构敌人

    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];
                }
^我要删除/替换为创建敌方结构的代码。

此代码应为

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

请注意,方括号现在位于
结构
成员的末尾。

作为船舶矩阵的结构既笨拙又浪费。你不断地摆弄阵列,只是为了与单独的飞船一起工作。您不必分配一大块内存来容纳最大数量的船只。你需要复制敌人和友军的整个结构。你必须复制结构中的所有内容才能使用一艘船

相反,创建一个
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);
}
#包括
#包括
/*存放船只的建筑物*/
类型定义结构{
int x;
int-y;
整数距离;
国际卫生组织;
int型;
}船舶;
/*无论结构多么简单,都要将函数写入
创造并摧毁它。这使得使用它更简单,而且
屏蔽您的代码,防止将来对结构进行更改*/
Ship*Ship_new(){
/*使用calloc()而不是malloc()来保证一切
初始化为0,而不是处理垃圾*/
返回calloc(1,尺寸(船));
}
无效船舶(船舶*船舶){
免费(船);
}
/*常量比宏更容易调试*/
const int MAX_friends=200;
const int MAX_敌人=200;
int main(){
/*只存储一个指向船只的指针列表。这是更多
灵活,节省大量内存*/
友谊赛[最大的友谊赛];
船*敌人[最大敌人];
/*为演示目的制造新船舶*/
船*敌=新船();
Ship*friendly=Ship_new();
/*只是为了演示设置值*/
敌人->x=5;
敌人->y=10;
敌人->生命=100;
敌人->类型=5;
友好->x=99;
友好->y=23;
友好->健康=50;
友好->类型=10;
/*把他们分配到他们的名单上。因为这是一个船的名单*
我们只需要复制指针,而不是所有的数据*/
友人[0]=友人;
敌人[0]=敌人;
/*使用它们。友人[0]和友人指向
同一艘船,不是副本*/
printf(“友好的#1健康:%d\n”,友好的[0]->健康);
printf(“敌方#1生命值:%d\n”,敌方[0]->生命值);
}

很抱歉延迟了回复,正如@Schwern所说的,这个结构在其他事情中显得相当笨拙。最后,我发现最简单的方法是为每个属性编写单独的、更小的函数

不过,再次感谢各位的回复:)

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