Difficulty with dynamic struct allocation

Asked

Viewed 53 times

0

The question is : implement a type of Tbaralho dice to represent a pile of cards. Your data type should store the cards in the deck and the amount of cards present. Consider that the maximum size of a deck is limited by the main memory (you must use allocation dynamics) and should grow by 10 elements (see function in C realloc1, will not be allowed the use of chaining). Use one-dimensional arrays (vectors) to implement ADT.

I can dynamically allocate one struct within another struct ?

typedef struct carta{
    char naip;
    int num;
} TCarta;

typedef struct baralho{
    TCarta *vet_carta;
    int qtd_carta;
} TBaralho; //baralho

void inicializar (TBaralho *pBaralho)
{
    pBaralho = (TBaralho*) malloc(10*sizeof(TBaralho));
    pBaralho->qtd_carta=0;
}
  • Yes, in the same way as for pBaralho. For example: pCarta = (Tcarta*) malloc(10sizeof(Tcarta)); or pBaralho[i]. vet_carta = (Tcarta) malloc(10*sizeof(Tcarta));

  • The tip was very good, while I sent this post I was testing and getting.

  • 
if (pBaralho->qtd_carta==52 && pBaralho->vet_carta[pos]=NULL)
 {
 return 0;
 }
 How do I test if a struct vector at position X is empty ? That way I put it didn’t work =/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.