There are some possibilities for relational C structures.
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 part of the player structure.
Advantages: the code is simpler and the layout of the memory 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 circumvented by creating a special instance of animal that represents the absence of the animal (for example, with id == -1).
Another possibility of a "weaker" relation is to make use of 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 dynamically allocated in memory.
Disadvantages: more complex code.
Advantages: flexibility, making it possible for each player to have their own number of animals.
Have you learned lists? You can make a *Tanimal list and put this list in your Tjogador struct. Posting the rest of the code makes it easier to implement and will improve the question. If you haven’t learned lists you can still use an array instead, if allowed in your problem.
– George Wurthmann