简体   繁体   中英

Creating an object / typedef struct using dynamic arrays in C

I am trying to create an object (typedef struct) in C from multiple dyanimc arrays, but I'm having a few issues assigning values to the members, my code is below:

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

^ define MAX_SHIPS and create the struct enemy.

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

At the moment I get the error 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++;                
                }
            }
        }

^ Code I want to remove / replace with the creation of the enemy struct.

A struct that is a sort of matrix of ships is awkward and wasteful. You're constantly messing with arrays just to work with individual ships. You don't have to allocate a big block of memory to hold the max amount of ships. You need to copy the whole struct for enemies and friendlies. You have to copy everything in and out of the struct just to work with one ship...

Instead, make a single Ship struct. Then you can pass just the pointer around and use the same struct for both friendly and enemy ships. You can keep friendly and enemy ships in lists of Ship pointers without copying all the data.

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

This code should be

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

Note that the square brackets are now at the end of the struct members.

sorry for the delayed reply / response, as @Schwern said, the struct turned out to be fairly awkward amongst other things. In the end I found the easiest thing to do was write separate, smaller functions for each of the attributes.

But again, thanks very much for the responses guys :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM