在 C 中使用动态数组创建对象/typedef 结构

标签 c arrays struct

我正在尝试从多个 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++;                
                }
            }
        }

^ 我想删除/替换为创建敌人结构的代码。

最佳答案

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

相反,创建单个 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);
}

关于在 C 中使用动态数组创建对象/typedef 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41685147/

相关文章:

c - 理解定点运算

c - 在 C/C++ 中只获取一个简单的数字

arrays - 确定跨步数组是否重叠的算法?

c - 为什么此 C 代码片段中的 NULL 取消引用不会导致未定义的行为

c++ - 将 const 结构引用转换为非常量指针

C89 - 错误 : expected ')' before '*' token

使用 wasd 键更改 x 和 y 值的 C 程序

c - 在函数声明中引用结构类型?

C++,函数将数组作为字符串返回

objective-c - 是否可以对比 int 所能容纳的更多的值进行位掩码?