使用C中的动态数组创建object / typedef结构

时间:2017-01-16 21:19:32

标签: 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++;                
                }
            }
        }

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

3 个答案:

答案 0 :(得分:1)

作为一种船舶矩阵的结构是笨拙和浪费的。你经常搞乱阵列只是为了与个别船只一起工作。您不必分配大块内存来容纳最大数量的船只。你需要为敌人和友谊复制整个结构。你必须将所有内容复制到结构中,才能与一艘船一起工作......

相反,制作一个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);
}

答案 1 :(得分:0)

此代码应为

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成员的末尾。

答案 2 :(得分:0)

对于延迟的回复/回复感到抱歉,正如@Schwern所说,结构在其他方面变得相当尴尬。最后,我发现最简单的事情就是为每个属性编写单独的小函数。

但是,再次感谢回复家伙:)