2017-01-16 5 views
1

複数のdyanimc配列からCでオブジェクト(typedef struct)を作成しようとしていますが、メンバーに値を割り当てる際に問題があります。 :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; 

^構造体の敵を作成します。

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が表示されます。

^コード敵の構造体の作成で削除/置換したいと思います。

答えて

0

@Schwernが言ったように、構造体は、とりわけ、かなり厄介であることが判明。最後に、最も簡単なことは、属性ごとに別々の小さな関数を書くことでした。

もう一度、ありがとうございました。回答者の方々:

0

このコードは、角括弧はstruct部材の端部になっていること

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

注意すべきです。

1

船の行列の一種である構造体は、扱いにくく無駄です。あなたは、個々の船で作業するために常にアレイを迷惑しています。あなたは最大量の船を保持するために大きなメモリブロックを割り当てる必要はありません。あなたは敵と友軍のために構造体全体をコピーする必要があります。 1つの船で作業するために、構造体の内外をすべてコピーする必要があります。

代わりに、Ship構造体を1つ作成します。その後、ポインタを渡して、味方船と敵艦の両方に同じ構造体を使用することができます。あなたはすべてのデータをコピーすることなく、友軍と敵の船を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); 
} 
関連する問題