I don’t understand the following structure

Asked

Viewed 51 times

1

I’m having trouble understanding the following structure:

typedef struct TipoItem{
   int Chave;
} Item;

typedef struct TipoCelula *Apontador;   //aqui temos um apontador tipo celula.

typedef struct TipoCelula{
    Item Item;
    Apontador Prox; //      NAO ESTOU ENTENDENDO ESTA PARTE, pois apontador 
} Celula;           //      APONTADOR É UM PONTEIRO E AQUI TEMOS PROX QUE É DO TIPO 
                    //      APONTADOR?

typedef struct TipoPilha{
    Apontador Topo;
    int tam;
}Pilha;

1 answer

1

You created an alias called Pointer for the type

struct TipoCelula *

Thus the members of his struct TipoCelula just below are actually:

struct TipoCelula {
   Item item; 
   struct TipoCelula *prox; // Ponteiro para um outra estrutura do tipo TipoCelula
}

This is a typical construction used in data structures like stacks, queues, etc...

Remember that the typedef, despite the name, it does not create a new type, it just renames pre-existing types or structures in order to facilitate the reading of the code ( sometimes even disturbs, leaving it more confused by exactly losing the meanings when misused or used too much).

Browser other questions tagged

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