Relate structs in C

1

Hello, I have to do the relation between structs in a work in C. I have a struct player with the fields name and code and an animal struct that has the fields name and code. I can normally insert players and animals each into their struct.

struct jogador{
   int codigo;
   char nome;
}TJogador;

struct animal{
   int codigo;
   char nome;
}TAnimal;

However, each player can have several animals and I need to relate these two structs so that when a player has an animal that is already inserted in the animal struct he only refers to this animal so when I list the players already list the animals of it.

Can someone give me a light?

    
asked by anonymous 15.03.2017 / 01:08

2 answers

3

Do with chain or vector list. Example:

    struct animal{
       int codigo;
       char nome[200];
    }TAnimal;

    struct jogador{
       int codigo;
       char nome[200];
       // o numero de animais
       int numeroDeAnimais;
       // as referencias aos animais cadastrados.
       TAnimal* animal[200];
    }TJogador;

You must initialize the numeroDeAnimais field with the value 0 and when adding an animal, you must increment the value of the numeroDeAnimais field and add the animal to the end of the animal vector.

    
15.03.2017 / 15:58
2

There are some possibilities for relational structures in C.

For a more "strong" reference, create a fixed-size vector of animals in the player structure:

typedef struct animal {
   int codigo;
   char nome;
} TAnimal;

#define MAX_ANIMAIS 2
typedef struct jogador{
  int codigo;
  char nome;
  TAnimal animais[MAX_ANIMAIS];
} TJogador;

You can add animals to each player with:

TJogador jogador;
jogador.animais[0].codigo = 1;
jogador.animais[0].nome = 'a';
jogador.animais[1].codigo = 2;
jogador.animais[1].nome = 'b';
// ...
int i;
for (i = 0; i < MAX_ANIMAIS; i++) {
  printf("TAnimal{%d %c}\n", jogador.animais[i].codigo,
          jogador.animais[i].nome);
}

In this way, every player has a fixed number of animals (2 in the example) and the animal data will be parts of the player structure.

Advantages: The code is simpler and the memory layout is ideal if the animals are accessed whenever the player is accessed. Disadvantages: less flexible, every player must have a fixed number of animals. This can be bypassed by creating a special animal instance that represents the animal's absence (for example, with id == -1).

Another possibility of a "weak" relationship is to use pointers. For example:

typedef struct animal {
   int codigo;
   char nome;
} TAnimal;

typedef struct jogador {
  int codigo;
  char nome;
  int num_animais;
  TAnimal* animais;
} TJogador;

Animals can be accessed with:

TJogador criaJogador(int codigo, char nome, int num_animais) {
  TJogador jogador;
  jogador.codigo = codigo;
  jogador.nome = nome;
  jogador.num_animais = num_animais;
  jogador.animais = (TAnimal*)malloc(sizeof(TAnimal) * num_animais);
  return jogador;
}

TJogador destroiJogador(TJogador* jogador) {
  free((*jogador).animais);
  (*jogador).num_animais = 0;
}

int main(int argc, char* argv[]) {
    TJogador jogador = criaJogador(1, 'a', 2);
    jogador.animais[0].codigo = 1;
    jogador.animais[0].nome = 'a';
    jogador.animais[1].codigo = 2;
    jogador.animais[1].nome = 'b';
    // ...
    int i;
    for (i = 0; i < jogador.num_animais; i++) {
       printf("TAnimal{%d %c}\n", jogador.animais[i].codigo,
              jogador.animais[i].nome);
    }
    destroiJogador(&jogador);
}

In this way, every player has a variable number of animals and the animal data will be allocated dynamically in memory.

Disadvantages: more complex code. Advantages: flexibility, being possible for each player to have their own number of animals.

    
15.03.2017 / 16:21